if condition with nullable

前端 未结 10 1163
抹茶落季
抹茶落季 2021-01-02 10:20

There is a lot of syntax sugar with Nullable like those:

int? parsed to Nullable

int? x = null
   if (x != null) // Parsed          


        
10条回答
  •  我在风中等你
    2021-01-02 11:05

    It's because a nullable bool cannot be implicitly converted to a bool. It's the same reason why you can do this :

    int x1 = 7;
    int? x2 = x1;
    

    but you can't do this :

    int? x1 = 7;
    int x2 = x1;
    

    you have to check before if it contains a value. Implicitly converting with nullable types can only be done when assigning a value, not when using that value.

提交回复
热议问题