Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Nullable<bool> type?

a bool variable could hold true or false, while bool? could be null as well.

Why would we need a third value for bool ? If it is not true, what ever it is, it is == false

Can you suggest a scenario where I would fancy a bool? instead.

Thanks

like image 608
Asad Avatar asked Feb 05 '10 01:02

Asad


People also ask

Is nullable bool a reference type?

So, C# 2.0 provides a special feature to assign a null value to a variable that is known as the Nullable type. The Nullable type allows you to assign a null value to a variable. Nullable types introduced in C#2.0 can only work with Value Type, not with Reference Type.

How do you use nullable types?

Nullable types can only be used with value types. The Value property will throw an InvalidOperationException if value is null; otherwise it will return the value. The HasValue property returns true if the variable contains a value, or false if it is null. You can only use == and !=

Why do we use nullable types in C#?

We are using nullable types when we need to represent an undefined value of an underlying type. While Boolean values can have either true or false values, a null in this case means false as there is no undefined value. When you have a database interaction, a variable value can be either undefined or missing.

What do you mean by nullable type?

Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type.


1 Answers

Something can be true, false or undefined for many reasons. How would one answer "Is your third child a girl?" if one only has two children? Both true and false are incorrect. Null would be appropriate as saying that the comparison doesn't apply.

like image 188
Lee Avatar answered Oct 26 '22 14:10

Lee