TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# DateTime.MinValue: Null

This C# article shows how to use the DateTime.MinValue field to initialize DateTime instances.

DateTime.MinValue. DateTime cannot be assigned to null.

It has a default value that is equal to DateTime.MinValue. The DateTime type is a value type—just like an integer. Instead of null assignment, we can use the DateTime.MinValue field.

Note: You cannot assign null to DateTime. It is easiest to initialize to DateTime.MinValue.

Example. This example 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.

ClassNull

C# program that shows null DateTime and MinValue

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

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

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.

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

Commented method. 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.

And: If this doesn't make sense to you, think of it in the same way as assigning an int to null.

MinValue. Instead of assigning the DateTime to null, you can use the readonly field DateTime.MinValue. In the base class library, the MinValue field is a public static readonly field. It won't change throughout your program's execution.

Public Static Readonly Field

Null. 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.

And: The DateTime type never points at anything. It stores all its data in place of that reference, directly in the variable itself.

Nullable DateTime

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

Summary. We looked at some issues relating to null DateTime variables, and also how you can use the DateTime.MinValue field to initialize new DateTimes. There are relevant aspects of reference and value types.

And: We looked at the exact value of DateTime.MinValue, which is "1/1/0001 12:00:00 AM" (a long time ago).


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf