Nullable DateTime in C#

北城以北 提交于 2021-01-28 18:21:14

问题


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 
    

回答1:


The second statement 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;



回答2:


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.




回答3:


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;



回答4:


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)




回答5:


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;



回答6:


if you want to cast null to nullable datetime then you can use

DateTime? dt = (DateTime?)null;


来源:https://stackoverflow.com/questions/36300160/nullable-datetime-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!