253

In order to check if a Type ( propertyType ) is nullable, I'm using:

bool isNullable =  "Nullable`1".Equals(propertyType.Name)

Is there some way that avoid using magic strings ?

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • 2
    Check if a type is `Nullable`? If so, have a look at http://stackoverflow.com/questions/374651/how-to-check-if-an-object-is-nullable – Russ Cam Jan 20 '12 at 10:27
  • 6
    This question is not duplicated. It asks for if type is nullable (not object is nullable) – M.Hassan Sep 07 '19 at 05:17

2 Answers2

514

Absolutely - use Nullable.GetUnderlyingType:

if (Nullable.GetUnderlyingType(propertyType) != null)
{
    // It's nullable
}

Note that this uses the non-generic static class System.Nullable rather than the generic struct Nullable<T>.

Also note that that will check whether it represents a specific (closed) nullable value type... it won't work if you use it on a generic type, e.g.

public class Foo<T> where T : struct
{
    public Nullable<T> Bar { get; set; }
}

Type propertyType = typeof(Foo<>).GetProperty("Bar").PropertyType;
// propertyType is an *open* type...
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Looks like you've found a very interesting disagreement between the specification (your link in the first line) and the actual behavior. For your `propertyType` object from the last line is a counter-example. What I mean is that `Nullable.GetUnderlyingType(propertyType)` does **not** return null (it returns your `T`) although `propertyType` is an _open_ generic type (`propertyType.ContainsGenericParameters`). I experimented a bit, and it looks like an open generic type with ``Nullable`1`` is OK _unless_ the type parameter is exactly the `T` from `typeof(Nullable<>).GetGenericArguments()[0]`. – Jeppe Stig Nielsen Dec 03 '12 at 16:07
  • What do you mean by "it returns your T"? This sounds like it's worth adding as a new question, with a short but complete example. – Jon Skeet Dec 03 '12 at 16:43
  • @JonSkeet I would have expected `Nullable.GetUnderlyingType( typeof( string ) ) != null` to return true. Am I missing something? – EL MOJO Sep 11 '18 at 21:01
  • 3
    @ELMOJO Why would you expect that? String isn't a nullable value type. – Jon Skeet Sep 11 '18 at 22:09
  • 2
    @JonSkeet Yeah, I realized that about 15 minutes after I typed it. One of those momentary lapses in reasoning. – EL MOJO Sep 12 '18 at 20:44
  • @JonSkeet string is definitely nullable – Bojidar Stanchev Mar 06 '20 at 13:26
  • 4
    @BojidarStanchev: It's not a nullable *value* type though. (And in C# 8 with nullable reference types enabled, it's not even a nullable type :) Then again, what would I know - I use `var` all the time in C#, contrary to the implied advice in your profile. Doesn't seem to be hurting my career so far. – Jon Skeet Mar 06 '20 at 13:28
  • What about `default(T) == null`? Are there any pitfalls with that assumption? – Paul Feb 08 '23 at 12:46
  • @Paul: It depends on what you're trying to achieve. The question was written long before nullable reference types existed, so I'm loathe to speculate further, to be honest. – Jon Skeet Feb 08 '23 at 12:57
73

Use the following code to determine whether a Type object represents a Nullable type. Remember that this code always returns false if the Type object was returned from a call to GetType.

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {…}

explained at the below MSDN link:

http://msdn.microsoft.com/en-us/library/ms366789.aspx

Moreover, there is a similar discussion at this SO QA:

How to check if an object is nullable?

Community
  • 1
  • 1
S2S2
  • 8,322
  • 5
  • 37
  • 65
  • Thank you for the link to the other SO question. There is a mountain of information on the other question. – JamesHoux Aug 22 '19 at 01:23
  • It would be good to add `!type.IsGenericTypeDefinition` is well. Else this will return true for `typeof(Nullable<>)`, which may or may not be desired, just saying – nawfal Aug 15 '20 at 16:23