Why doesn't this C# code compile?

后端 未结 4 1279
离开以前
离开以前 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:43

    The left hand side of the assignment is not used when deducing the type of an ?: expression.

    In b ? A : B, the types of A and B must either be the same, or one must be implicitly convertible to the other.

提交回复
热议问题