System.DateTime? vs System.DateTime

后端 未结 6 1065
心在旅途
心在旅途 2021-02-05 02:53

I was writing to some code where I needed to read the date value from a Calendar control in my page (Ajax toolkit: calendar extender).

The code below:

6条回答
  •  清酒与你
    2021-02-05 03:46

    ? means that the type is nullable. For details, see e.g. MSDN

    Nullable is a compiler-supported wrapper around value types that allows value types to become null.

    To access the DateTime value, you need to do the following:

    DateTime? dateOrNull = myCalendarExtender.SelectedDate;
    if (dateOrNull != null)
    {
        DateTime newSelectedDate = dateOrNull.Value;
    }
    

提交回复
热议问题