What is the big difference? casting or testing the difference in values ​​in an if statement

In this case, my Truth variable is a variable with a null value. My question is simple

Will something be spelled wrong?

if ((bool)Truth) {}

or should i stick with this?

if (Truth == True) {}

Never worked with variables with null values ​​not defined, if there is even a difference different from the other, is it possible if ((bool) Truth) {} took one more step?

+4
source share
3 answers

. Truth - null, InvalidOperationException: Nullable object must have a value . , true . null , .

+5

, Jesse Slicer, , , , . , .

:

if (myBool.HasValue && myBool.Value)

, myBool (HasValue) true (Value ). , , if, nullable bool null.

, HasValue Value. Value, , , .

+3

,

if ((bool)Truth) {}

- bool, , bool, null, .

,

if (Truth == true) {}

Truth == null

,

Truth == false

? , . null , false, .

Nullable HasValue Value. HasValue == true Value.

0
source

All Articles