Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Nullable in conditional statement [duplicate]

Why do we need explicit cast in second statement?

bool? a = null; bool b = false; bool c = true;

1.) if(b || c) a = b; else a = null;

2.) a = (b || c)?(Nullable<bool>)b:null;

like image 297
c161c Avatar asked May 02 '26 11:05

c161c


1 Answers

The conditional operator is an expression, thus it needs a return type - also both cases have to have the same return type. In your case, there is no way of determining the return type automatically, thus you need to cast.

like image 129
Femaref Avatar answered May 05 '26 00:05

Femaref