Rss Feed Like Us on facebook Google Plus

June 28, 2016

Why is null not allowed for DateTime in C#?

DateTime is a value-type (struct), where-as string is a reference-type (class etc). That is the key difference. 

A reference can always be null; a value can't (unless it uses Nullable<T> - i.e. DateTime?), although it can be zero'd (DateTime.MinValue), which is often interpreted as the same thing as null 


C# that causes null DateTime error

using System;

class Program
{
    static void Main()
    {
 DateTime current = null;
    }
}

Results

error CS0037: Cannot convert null to 'DateTime'
because it is a non-nullable value type

© 2011-2016 Techimpulsion All Rights Reserved.


The content is copyrighted to Tech Impulsion and may not be reproduced on other websites.