4

Possible Duplicates:
How to check if an object is nullable?
Determine if reflected property can be assigned null

How can I properly identify if a variable (or a class member) of given type can take null? More specifically how to handle Nullable<T> since it is not a reference type? Or any other type that may have some weirdo implicit conversion defined on it. My gut feeling is that the only sure way to find out is to try{} catch{} it and see if it blows up... But maybe there are some tricks to it.

Community
  • 1
  • 1
Ilia G
  • 10,043
  • 2
  • 40
  • 59

4 Answers4

12

It's not clear what information you do have about the type in question.

try/catch will do things at execution time, which isn't really what you want.

For a concrete type, you should be able to know just by knowing the variable type. It should be pretty obvious - or if it's not, you've got bigger problems than not knowing about nullity :)

For a generic type, I've found this is quite useful:

if (default(T) == null)

For a Type reference (e.g. if you're using reflection) you could use:

if (!type.IsValueType || Nullable.GetUnderlyingType(type) != null)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I liked the `if (default(T) == null)`. Then I realized I was asking wrong question. I should be trying to assign `default(T)` instead of `null` in the first place – Ilia G Mar 10 '11 at 16:10
  • @liho1eye: It's hard to know whether that's appropriate, because we don't know what your context is. If you could give an example of your code, that would really help. – Jon Skeet Mar 10 '11 at 16:13
  • No no, that is good. Seems obvious in retrospect... not sure why I was fixated on `null`. – Ilia G Mar 10 '11 at 16:19
1

Only reference types can actually hold a null reference; the special case of Nullable<T> is all syntactic sugar; the resulting value is not actually null (since Nullable<T> is a value type, too, so it cannot hold a null reference), it's just a Nullable<T> with default(T) as its Value and HasValue = false.

So it depends on what you're asking.

If you're trying to determine if the type qualifies for in-code assignment to the null (Nothing in VB.NET) literal, then it's:

  • All reference types
  • Nullable<T>

If you're trying to determine if the type qualifies to hold a bona-fide null reference, then it's

  • All reference types

As for using reflection to inspect a particular type at runtime, checking IsValueType should be enough to get you what you need, even if it's the former (just add a special case in your code for Nullable<T>).

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • It depends what you mean by "null value". If you only said "null reference" then I'd entirely agree with you. However, the C# spec refers to things like this: "An instance for which HasValue is false is said to be null." and "A nullable type can represent all values of its underlying type plus an additional null value." So the term "null value" *is* used in the context of nullable value types. – Jon Skeet Mar 10 '11 at 16:17
  • (Additionally, even the term "nullable type" is somewhat ambiguous - sometimes it's used to mean "nullable value type" and sometimes it's used to mean "nullable value types and reference types". I think we should blame Eric for this ambiguity, just on the principle of the thing :) – Jon Skeet Mar 10 '11 at 16:18
  • @Jon: Good point; I've tried to clarify my answer to reflect the distinction, though it's a little hard to draw a clear line between "null value" and "null reference" in a short answer! – Adam Robinson Mar 10 '11 at 17:51
1
bool canBeNull = !type.IsValueType || (Nullable.GetUnderlyingType(type) != null);
CD..
  • 72,281
  • 25
  • 154
  • 163
0

Nullable exposes the HasValue property so if you want to check if a value type is nullable then just check if it exposes that Property.

Peter Kelly
  • 14,253
  • 6
  • 54
  • 63