Is Nullable - CSharp System

CSharp examples for System:Int32

Description

Is Nullable

Demo Code


using System.Globalization;
using System.Collections.Generic;
using System.Collections;
using System;//w w w.j  a v a  2 s  . co  m

public class Main{
        /// <summary>
      /// 
      /// </summary>
      /// <typeparam name="U"></typeparam>
      /// <param name="obj"></param>
      /// <returns></returns>
      public static bool IsNullable<U> (object obj) where U : struct
      {
         if (obj == null)
            return false;

         return obj is Nullable<U>;
      }
        /// <summary>
      /// 
      /// </summary>
      /// <param name="obj"></param>
      /// <returns></returns>
      public static bool IsNullable (object obj)
      {
         var type = obj.GetType ();

         return (type.IsGenericType
                 && type.GetGenericTypeDefinition ().Equals (typeof (Nullable<>)));
      }
}

Related Tutorials