I am trying to assign null
value to Boolean variable but it is not taking it
bool b = null;
Boolean variable cannot have a value of null - is a correct one. We can create a Boolean variable which have null value by assigning it "-1" that represents "null Boolean value". Boolean variable cannot have a value of null because it is a non-nullable value type. Answer is 1 & 5.
You could use a string instead of boolean and set it to "False", "True" or "" where the empty string represents your null value. Alternatively you need for every Boolean an extra Boolean like <attribute>IsSpecified which you set to false if the attribute value false means null.
Yep - it can be null. Your Boolean column is supposed to be only true or false . But now you're in the madness of three-state Booleans: it can be true , false , or NULL .
We cannot assign null to int, bool, double, char values while retrieving values from the database. So it is recommended to use nullable types instead of value types in a case where we want to store the null value.
You need to use a nullable bool:
bool? b = null;
C# has two different categories of types: value types and reference types. Amongst other, more important distinctions, value types, such as bool or int, cannot contain null values.
You can, however, use nullable version of value types. bool?
is a C# alias for the .NET Nullable<bool>
type (in the same way string
is an alias for String
) and can contain null values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With