[C#] The Nullable paradox, part 2

In the previous part, we saw that we can't use nullable types with generic constraints. I gave the beginning of an explanation, but now we'll see the full story. typeof(T) vs T.GetType() Everybody knows that for any value type T, the following: T value; value.GetType() is always…

READ MORE

[C#] The Nullable paradox, part 1

Did you ever try to use a Nullable as a generic type parameter ? Well it works as expected... as soon as you don't have any type constraint. A reference type ? Let's try with a simple example with a class constraint. T DoSomething<T>() where T : class { return null;…

READ MORE

[C#] I wish I knew : Nullable&lt;T&gt;.GetDefaultValue(T)

I've been a professional C# developer for years now, but I'm still discovering trivial stuffs. In "I wish I knew", I describe a feature that I missed. Given the following: struct T {} T? nullable; T fallback; I always though the only alternative to this: T value = nullable.HasValue ? nullable.Value…

READ MORE