Translate

Saturday, July 7, 2012

C# check if DateTime variable is null



using System;
using System.Text;

namespace ConsoleApplication
{

    class Program
    {
        static void Main(string[] args)
        {

            DateTime dt1 = new DateTime();
            DateTime? dt2=null ;

            isDateTimeNull(dt1);
            isDateTimeNull(dt2);

        }

        static void isDateTimeNull(DateTime? dt)
        {
            if(dt.GetHashCode()==0)
            {
                Console.WriteLine("DateTime is unassigned");
            }
        }
    }
}


Output:










In stackoverflow.com I found another way for checking null datetime


 DateTime datetime = new DateTime();
 if (datetime == DateTime.MinValue)
 {
    //unassigned
 }




1 comment:

Comments will appear once they have been approved by the moderator