wonea
wonea

Reputation: 4969

Querying a Nullable bool in C#

I've come across a curious problem with the following code. It compiles fine although Resharper highlights the code segment (autorefresh == null), notifying me Expression is always false

bool? autorefresh = Properties.Settings.Default.autorefresh;
autorefresh = (autorefresh == null) ? false : autorefresh;
Enabled = (bool)autorefresh;

Any ideas how better to get around this problem?

Edit 07/02/2012 16:52

Properties.Settings.Default.autorefresh

The above is a bool, not a string.

Upvotes: 4

Views: 1169

Answers (7)

Eric Lippert
Eric Lippert

Reputation: 660108

Reason it through:

bool? autorefresh = Properties.Settings.Default.autorefresh; 
                 // ^^^ this is a non-nullable Boolean

Properties.Settings.Default.autorefresh is non-nullable, therefore it will be either true or false.

Therefore the nullable local autorefresh will also be either true or false, since it is initialized to a value that is either true or false.

autorefresh = (autorefresh == null) ? false : autorefresh; 
                       // ^^^^ therefore this test will never succeed

Therefore this is equivalent to:

autorefresh = autorefresh; 

which is obviously pointless. (And, as others have pointed out, autorefresh ?? false is the better way to write this code anyway.)

The question is: why do you have the local variable in the first place? Why not simply say:

Enabled = Properties.Settings.Default.autorefresh;

?

Upvotes: 3

James
James

Reputation: 82096

I think what you want is:

Enabled = Properties.Settings.Default.autorefresh ?? false;

In light of your comments, it appears you were unneccessarily assigning the value of autorefresh to a Nullable<bool>. In terms of safeguarding the data, the Settings will return you the default value for that type if it is invalid or missing (which would be false for boolean's). Therefore, your code should simply be:

Enabled = Properties.Settings.Default.autorefresh;

Upvotes: 6

Tory
Tory

Reputation: 520

You could also jut do this:

 Enabled = Properties.Settings.Default.autorefresh.GetValueOrDefault(false);

No need to to check for nulls if the nullable value can do it for you.

Upvotes: 1

Pongsathon.keng
Pongsathon.keng

Reputation: 1435

The autorefresh is nullable type which means that autorefresh.Value can be null. I think that you can do like this

enable = !autorefresh.HasValue ? false : autorefresh.Value;

Upvotes: 0

EursPravus
EursPravus

Reputation: 343

You could try this:

bool? autorefresh = Properties.Settings.Default.autorefresh;
Enabled = (!autorefresh.HasValue) ? false : autorefresh.Value;

Upvotes: 0

CodexArcanum
CodexArcanum

Reputation: 4016

Nullables have some interesting behavior. I'd have to dig a bit to get you the exact reasons for what you see. Regardless, the correct way to test a nullable is to use the .HasValue method.

Upvotes: 0

Luis Filipe
Luis Filipe

Reputation: 8708

  bool? autorefresh = Properties.Settings.Default.autorefresh ?? false;

It is safe to make these following comparisons with nullable operators

autorefresh == null 

or you may also compare

autorefresh == true 

or

autorefresh == false

Upvotes: 1

Related Questions