Why doesn't this C# code compile?

后端 未结 4 1278
离开以前
离开以前 2020-12-11 16:23
double? test = true ? null : 1.0;

In my book, this is the same as

if (true) {
  test = null;
} else {
  test = 1.0;
}
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-11 16:34

    double? test = true ? (double?)null : 1.0;
    

    will work. That's because there is no conversion from the type of the first expression (null) to the type of the second expression (double).

提交回复
热议问题