0% found this document useful (0 votes)
39 views

C# Nullable Datetime

This document discusses how to assign null values to DateTime variables in C#. It shows an example of declaring a nullable DateTime variable and assigning null to it. The example then demonstrates checking for null using the HasValue property and providing a default value using GetValueOrDefault() if null is present.

Uploaded by

Manish Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

C# Nullable Datetime

This document discusses how to assign null values to DateTime variables in C#. It shows an example of declaring a nullable DateTime variable and assigning null to it. The example then demonstrates checking for null using the HasValue property and providing a default value using GetValueOrDefault() if null is present.

Uploaded by

Manish Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C# Nullable Datetime

In this article, we will discuss on How to assign null value


to Datetime in C#.
Lets look at the below example to understand nullable datetime in C#

1 using System;
2
3 class Program
4 {
5
static void Main()
6
{
7
//
8
// Declare a nullable DateTime instance and assign to null.
9
//
1
DateTime? value = null;
0
NullDatetimeProgram(value);
1
value = DateTime.Now;
1
NullDatetimeProgram(value);
1
value = DateTime.Now.AddDays(1);
2
NullDatetimeProgram(value);
1
//
3
// You can use the GetValueOrDefault method on nulls.
1
//
4
value = null;
1
Console.WriteLine(value.GetValueOrDefault());
5
}
1
6
static void NullDatetimeProgram(DateTime? value)
1
{
7
//
1
// This method uses the HasValue property.
8
// If there is no value, then the output will be zero.
1
//
9
if (value.HasValue)
2
{
0
Console.WriteLine(value.Value);
2
}
1
else
2
{
2
Console.WriteLine(0);

2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
}
0
}
3
}
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8

You might also like