Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ?? operator difference

Is there a difference between the '?? true' and the '== true' within an if statement?

bool? b = Jsonfile.GetBoolean("testval");
if (b ?? true) { }
if (b == true) { }
like image 277
gooleem Avatar asked Nov 28 '22 01:11

gooleem


1 Answers

Yes, there is.

b ?? true will match when b is null or true

b == true will match when b is not null and is true

The difference is in the first line of the table (when b is null)

b      b ?? true  b == true
====   ========== ==========
null   true       false
true   true       true
false  false      false
like image 73
Ivan Stoev Avatar answered Dec 10 '22 04:12

Ivan Stoev