Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
code formatting
Source Link
newfurniturey
  • 38.6k
  • 10
  • 99
  • 104

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

Example usage applied to the OP's question:

Nullable<DateTime> foo; foo = true ? default(DateTime) : new DateTime(0); 

Example usage with the current accepted answer:

DateTime? foo; foo = true ? default(DateTime) : new DateTime(0); 

Also, by using default, you do not need to specify the variable as nullablenullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value (in the case of DateTime, it is null) and no error will be encountered. Example:

DateTime foo; foo = true ? default(DateTime) : new DateTime(0); 

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

Example usage applied to the OP's question:

Nullable<DateTime> foo; foo = true ? default(DateTime) : new DateTime(0); 

Example usage with the current accepted answer:

DateTime? foo; foo = true ? default(DateTime) : new DateTime(0); 

Also, by using default, you do not need to specify the variable as nullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value (in the case of DateTime, it is null) and no error will be encountered. Example:

DateTime foo; foo = true ? default(DateTime) : new DateTime(0); 

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

Example usage applied to the OP's question:

Nullable<DateTime> foo; foo = true ? default(DateTime) : new DateTime(0); 

Example usage with the current accepted answer:

DateTime? foo; foo = true ? default(DateTime) : new DateTime(0); 

Also, by using default, you do not need to specify the variable as nullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value and no error will be encountered. Example:

DateTime foo; foo = true ? default(DateTime) : new DateTime(0); 

Another solution similar to the accepted is to use C#'s default keyword. While defined using genericsgenerics, it is actually applicable to any type.

Example usage applied to the OP's question:

Nullable<DateTime> foo; foo = true ? default(DateTime) : new DateTime(0); 

Example usage with the current accepted-answer answer:

DateTime? foo; foo = true ? default(DateTime) : new DateTime(0); 

Also, by using default, you do not need to specify the variable as nullablenullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value (in the case of DateTime, it is null) and no error will be encountered. Example:

DateTime foo; foo = true ? default(DateTime) : new DateTime(0); 

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

Example usage applied to the OP's question:

Nullable<DateTime> foo; foo = true ? default(DateTime) : new DateTime(0); 

Example usage with the current accepted-answer:

DateTime? foo; foo = true ? default(DateTime) : new DateTime(0); 

Also, by using default, you do not need to specify the variable as nullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value (in the case of DateTime, it is null) and no error will be encountered. Example:

DateTime foo; foo = true ? default(DateTime) : new DateTime(0); 

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

Example usage applied to the OP's question:

Nullable<DateTime> foo; foo = true ? default(DateTime) : new DateTime(0); 

Example usage with the current accepted answer:

DateTime? foo; foo = true ? default(DateTime) : new DateTime(0); 

Also, by using default, you do not need to specify the variable as nullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value (in the case of DateTime, it is null) and no error will be encountered. Example:

DateTime foo; foo = true ? default(DateTime) : new DateTime(0); 
Source Link
newfurniturey
  • 38.6k
  • 10
  • 99
  • 104

Another solution similar to the accepted is to use C#'s default keyword. While defined using generics, it is actually applicable to any type.

Example usage applied to the OP's question:

Nullable<DateTime> foo; foo = true ? default(DateTime) : new DateTime(0); 

Example usage with the current accepted-answer:

DateTime? foo; foo = true ? default(DateTime) : new DateTime(0); 

Also, by using default, you do not need to specify the variable as nullable in order to assign it a null value. The compiler will auto-assign the specific variable-type's default value (in the case of DateTime, it is null) and no error will be encountered. Example:

DateTime foo; foo = true ? default(DateTime) : new DateTime(0);