What is a "DateTime"? and not just DateTime in C #?

What is the difference between DateTime? and a DateTime (without a question mark) in C #?

+4
source share
3 answers

DateTime? may be null, not DateTime

+17
source

The question mark after the value type is an abbreviation for the Nullable<T> structure.

Represents an object, type - the type of the value, which can also be assigned null as a reference type.

The Nullable<T> structure allows you to wrap value types (e.g. DateTime , Int32 , Guid , etc.) and treat them like reference types in certain respects. This gets a little more complicated (in terms of destination, dropped operators, etc.), and therefore I would recommend that you read the Nullable Types (C # Programming Guide) and related articles.

Null types are instances of System.Nullable struct. A null type can represent a normal range of values ​​for its base value type, plus an optional null value. For example, a Nullable<Int32> , pronounced "Nullable of Int32" can be assigned any value from -2147483648 to 2147483647, or it can be assigned a null value. A Nullable<bool> can be set to true, false, or zero. The ability to assign null values ​​to numeric and boolean types is especially useful when working with databases and other data types containing elements that cannot be assigned a value. For example, a Boolean field in a database may store true or false, or it may be undefined.

+19
source

DateTime? is another way to write Nullable <DateTime>. I suggest you read this to learn more about nullable:

Nullable (t) structure

+2
source

All Articles