简体   繁体   中英

Nullable DateTime in C#

I have two questions related to DateTime assingments

DateTime? y = 1 == 1 ? null: DateTime.MaxValue;
DateTime? y = null; // assignment works as expected
  • Why the first assignment issues error of type conversion between null and DateTime?
  • Which is the preferred way for null assignments of DateTime? in c#.

     DateTime? x = default(DateTime?); //prints null on console DateTime? x = null; // prints null on console DateTime? x = DateTime.MinValue; //print 01/01/0001 

The second statement DateTime? y = null; DateTime? y = null; is only an assignment of null to a nullable object.

Whereas the first is a conditional assignment, which assigns some value for the true state and some other value for the false; Here you are using the conditional operator for evaluating the condition. according to MSDN first_expression (executes if true) and second_expression* (executes if false)* must be of same type or an implicit conversion must exist from one type to the other. In our case both are different so The simple solution is doing an explicit conversion as like this:

DateTime? y = 1 == 1 ?(DateTime?) null : DateTime.MaxValue;

A1. Because in ternary operator both expressions/results should be of same type.

Acc. to MSDN Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

In your question, null and DateTime.MinValue do not match and hence the error conversion between null and DateTime .

You can do

DateTime? y = 1 == 1 ? null : (DateTime?)DateTime.MaxValue;

This way both answers return an answer whose type is DateTime? .

A2. Normally there is no said/preferred way of assigning this. This depends on user convention. All three are good and depend on user requirements.

Because ?: Operator operator expects same type on both sides.

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

So solution will be like below:

DateTime? y = 1 == 1 ? (DateTime?)null : DateTime.MaxValue;

And for second question, this will be good way for null assignment

DateTime? x = null;

DateTime? y = 1 == 1 ? null: DateTime.MaxValue;

This statement is giving an assignment error not because of the null assignment to a variable it is because of using null assignment in ternary operator and as you are using a class type here you the ternary operator do not lead you to do this illegal stuff as per CLR specifications mentioned,It might give you a straight compilation error.

//Which is the preferred way for null assignments of DateTime? in c#.

DateTime? x = default(DateTime?); //prints null on console

DateTime? x = null; // prints null on console

DateTime? x = DateTime.MinValue; //print 01/01/0001

As per Specifications and guidelines provided the Class Types should not be assigned null in any scenario so as per standard you can use the min value(though you can use default value too but it might effect in type conversions when needed)

The second one that you mentioned. You need to cast null value in this time asmensioned by Sir Nikhil Agrawal.

Ternary

  int y = 1;
  DateTime? dt3 = y == 1 ? (DateTime?)null : DateTime.MinValue;

Traditional way

       DateTime? dt3 = null;

        if (y == 1)            
             dt3 = null;            
        else
            dt3 = DateTime.MinValue;

如果要将null转换为可为null的日期时间,则可以使用

DateTime? dt = (DateTime?)null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM