How to remove union for boolean conditions?

I am trying to safely check if IList<> is not empty.

 var Foo = Bar.GimmeIListT(); // Returns an IList<SomeObject> if (Foo?.Any()) // Do cool stuff with items in Foo 

But there is an error with the condition:

Cannot implicitly convert 'bool?' to "bool". Explicit conversion exists (are you skipping listing?)

So it seems that the condition is being evaluated using a nullable bool, so I'm trying

 if (Foo?.Any().Value()) 

But this is also not useful:

'bool' does not contain a definition for "Value" and no extension .... blah blah blah

So, in the first case, he complains that it is a nullable bool, but in the second he complains that it is not.

As another prospect, I try:

 if (Foo?.Any() == true) 

This works - but it should not because it uses an implicit conversion, which in the first message said that it did not want to!

What's happening? What is the right way to do this?

+5
source share
3 answers

Can you compare with bool? if you use == , which is really the best / easiest approach:

 if (Foo?.Any() == true) ... 

as to why it is not allowed in if , but with == , John Skeet can explain this much better:

There is no implicit conversion from Nullable to bool. There is an implicit conversion from bool to Nullable and what happens (in terms of the language) with each of the bool constants in the first version. The bool == operator (Nullable, Nullable operator is then applied. (This is not quite the same as the other operators removed - the result is just bool, not Nullable.)

In other words, the expression 'fred == false' is of type bool, while the expression 'fred' is of type Nullable, so you cannot use it as the expression β€œif”.

So if allows only bool , but do you have bool? but operator == converts bool to bool? and can you compare two bool? .

+7
source

Edit

What seems to be causing the bool? is Foo?.Any() . If you do not want to compare it with true , I would suggest you have a temporary variable:

 bool? any = Foo?.Any(); if (any.Value) ... 

Alternatively, if the object is a class, you can use FirstOrDefault() != null as a validation condition. This will not take much time, because it will receive only the first object:

 if (Foot?.FirstOrDefault() != null)... 

I will use a temporary variable or parameter Foo?.Any() == true .

Original

Note. To my surprise, if (a?.Any()) cannot follow .Value() or .Value (!).

I think you need Value (property) without () (method):

 if (Foo?.Any()?.Value) ... 

bool? has a .Value (property), which is a bool .

+3
source

Any() returns bool , but Foo?.Any() will return bool?

So Foo?.Any().Value will not compile since Any() returns a bool that does not have a Value member.

If Foo is null , Any() will not execute because the statement will return null without interpreting the part after the ?. Operator ?. .

But if you put Foo?.Any() in paranthesis, you can work with a result like bool? and check it with Value or GetValueOrDefault() :

 (Foo?.Any()).GetValueOrDefault() 
+2
source

Source: https://habr.com/ru/post/1269935/


All Articles