79

I am reading a .NET book, and in one of the code examples there is a class definition with this field:

private DateTime? startdate

What does DateTime? mean?

reformed
  • 4,505
  • 11
  • 62
  • 88
Alvin S
  • 1,189
  • 1
  • 10
  • 16
  • Possible duplicate of https://stackoverflow.com/questions/2690866/what-is-the-purpose-of-a-question-mark-after-a-type-for-example-int-myvariabl – Fandango68 Oct 18 '17 at 05:58

7 Answers7

144

Since DateTime is a struct, not a class, you get a DateTime object, not a reference, when you declare a field or variable of that type.

And, in the same way as an int cannot be null, so this DateTime object can never be null, because it's not a reference.

Adding the question mark turns it into a nullable type, which means that either it is a DateTime object, or it is null.

DateTime? is syntactic sugar for Nullable<DateTime>, where Nullable is itself a struct.

shareef
  • 9,255
  • 13
  • 58
  • 89
Thomas
  • 174,939
  • 50
  • 355
  • 478
23

It's a nullable DateTime. ? after a primitive type/structure indicates that it is the nullable version.

DateTime is a structure that can never be null. From MSDN:

The DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 Anno Domini, or A.D. (also known as Common Era, or C.E.) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.)

DateTime? can be null however.

Pang
  • 9,564
  • 146
  • 81
  • 122
Daniel Auger
  • 12,535
  • 5
  • 52
  • 73
14

A ? as a suffix for a value type allows for null assignments that would be othwerwise impossible.

http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

Represents an object whose underlying type is a value type that can also be assigned a null reference.

This means that you can write something like this:

    DateTime? a = null;
    if (!a.HasValue)
    {
        a = DateTime.Now;
        if (a.HasValue)
        {
            Console.WriteLine(a.Value);
        }
    }

DateTime? is syntatically equivalent to Nullable<DateTime>.

Jorge Ferreira
  • 96,051
  • 25
  • 122
  • 132
10

It's equivalent to Nullable< DateTime>. You can append "?" to any primitive type or struct.

Ted Elliott
  • 3,415
  • 1
  • 27
  • 30
5

it basically gives you an extra state for primitives. It can be a value, or it can be null. It can be usefull in situations where a value does not need to be assigned. So rather than using for example, datetime.min or max, you can assign it null to represent no value.

mattlant
  • 15,384
  • 4
  • 34
  • 44
1

As we know, DateTime is a struct means DateTime is a value type, so you get a DateTime object, not a reference because DateTime is not a class, when you declare a field or variable of that type you cannot initial with null Because value types don't accept null. In the same way as an int cannot be null. so DateTime object never be null, because it's not a reference.

But sometimes we need nullable variable or field of value types, that time we use question mark to make them nullable type so they allow null.

For Example:-

DateTime? date = null;

int? intvalue = null;

In above code, variable date is an object of DateTime or it is null. Same for intvalue.

Sonu Rajpoot
  • 485
  • 4
  • 6
0
public class ReportsMapper : CommonMapper
{


    public DateTime? cb_Bill_From_Date { get; set; }

    public DateTime? cb_Bill_To_Date { get; set; }

    public DateTime? tff_Bill_From_Date { get; set; }
    public DateTime? tff_Bill_To_Date { get; set; }
}

If you declare DateTime As Null In Procedure Then You get an error stating DateTime Object Can never be Null so you need to add ? After DateTime that will say DateTime is Nullable too.

Hope This Help!