Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a bool variable is initialized in c#

Tags:

c#

boolean

So I have a bool variable like this in model.

bool Foo;

I am receiving data from server and de-serialize it model object. So whatever fields are not there in server data gets initialized to defaults. And default(bool) is false.

But value false is also an acceptable value for my variable Foo. So is there some way to check if it gets value false from server or its default.

I also have same issue with other types like int, double etc. which doesn't defaults to null.

like image 318
pratpor Avatar asked Dec 03 '22 18:12

pratpor


1 Answers

In your place, I'd use Nullable<bool> (bool?) to your purposes. Then you will be able to check value in this way:

bool? Foo;

if(Foo.HasValue)
{
   // do something with Foo.Value
}
else
{
   // Foo is uninitialized
}
like image 55
Fka Avatar answered Dec 11 '22 09:12

Fka