IsNullable

using System;
using System.Collections.Generic;
using System.Linq;


/// <summary>
/// Extension methods for the Type class
/// </summary>
public static class TypeExtensions
{
    /// <summary>
    /// Return true if the type is a System.Nullable wrapper of a value type
    /// </summary>
    /// <param name="type">The type to check</param>
    /// <returns>True if the type is a System.Nullable wrapper</returns>
    public static bool IsNullable(this Type type)
    {
        return type.IsGenericType 
               && (type.GetGenericTypeDefinition() == typeof(Nullable<>));
    }
}
Example:
// false
if (typeof(int).IsNullable())
{
...
}

// true
if (typeof(int?).IsNullable())
{
...
}

Description

Returns true if the type it is called upon is a System.Nullable<> wrapped type.

Details

Double click on the code to select all.

 

;