C# DateTime Null, Using DateTime.MinValue Default

by Sam Allen - Updated January 18, 2010

You want to know what the default value of DateTime variables is, and why they cannot be assigned to the null literal in the C# programming language. The DateTime type is a value type, and just like an integer, it cannot be assigned to null. Instead, use the DateTime.MinValue field. Here we look at the DateTime type and how you can assign it to DateTime.MinValue but not null, and what its default value in classes and arrays is.

You cannot assign null to DateTime.                       
... It is easiest to initialize to DateTime.MinValue.     
... In some cases, the default DateTime value is MinValue.

Using DateTime.MinValue and null

First, here we see an example that checks the initial value of a DateTime member variable. When you have a DateTime instance at the class level, it is not null, but is instead initialized to the DateTime.MinValue by default. After the example, we explore some issues related to this.

=== Program that shows null DateTime and MinValue (C#) ===

using System;

class Test
{
    DateTime _importantTime;
    public Test()
    {
        //
        // Test instance of DateTime in class.
        //
        Console.WriteLine("--- Instance DateTime ---");
        Console.WriteLine(_importantTime);
        Console.WriteLine(_importantTime == DateTime.MinValue);
    }
}

class Program
{
    static void Main()
    {
        //
        // Execute code in class constructor.
        //
        Test test = new Test();
        //
        // This won't compile!
        //
        // DateTime dateTime1 = null;
        //
        // This is the best way to null out the DateTime.
        //
        DateTime dateTime2 = DateTime.MinValue;
        //
        // Finished
        //
        Console.WriteLine("Done");
    }
}

=== Output of the program ===

--- Instance DateTime ---
1/1/0001 12:00:00 AM
True
Done

Description of the program. The program defines two classes, a Test class and the Program class. The Main entry point is defined in the Program class. Inside the Main method, a new instance of the Test class is allocated with the parameterless constructor.

Test class constructor. The "public Test()" constructor in the Test class accesses the _importantTime member variable of DateTime type. It writes the contents of the DateTime field to the screen, which results in "1/1/0001 12:00:00 AM". It then compares the value against DateTime.MinValue, which returns true.

Null DateTime error. In the Main method in the Program class, the line "DateTime dateTime1 = null" is commented out. This is because it won't compile, and the C# compiler will give you an error. If this doesn't make sense to you, think of it in the same was as assigning an int to null.

Using DateTime.MinValue. Instead of assigning the DateTime to null, you can assign DateTime instances to the readonly field DateTime.MinValue. In the base class library, the MinValue field is a public static readonly field, which means it won't change throughout your program's execution (it is basically a constant).

Null DateTime variables

When you try to assign a DateTime to the null literal in the C# language, The compiler will give an error "Cannot convert null to System.DateTime". The null value in the language is only used on reference types. It can tell the CLR that a reference doesn't point at anything. The DateTime type never points at anything; instead, it stores all its data in place of that reference, directly in the variable itself.

Error 1:
Cannot convert null to 'System.DateTime' because it is
a non-nullable value type

Summary

Here we looked at some issues relating to null DateTime variables, and also how you can use the DateTime.MinValue field to initialize new DateTimes. Additionally, we reviewed some aspects of reference and value types in the C# programming language. We looked at the exact value of DateTime.MinValue, which is "1/1/0001 12:00:00 AM" (a long time ago). We also saw how you can use DateTime as a class-level instance variable. Finally, please note that you can use a nullable type such as "DateTime?" to provide a logically null DateTime instance.

(See Nullable DateTime.)

(Do not copy this page.)

Dot Net Perls | Search
DateTime | DateTime Examples | DateTime Format Examples | DateTime.Now for Getting Current Time | DateTime.Parse String Method | DateTime.ParseExact Usage
C# | SaveFileDialog Tutorial | IntegralHeight Property (Windows Forms) | Array.FindIndex Method | File.Replace Method
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen