cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

C#.Net

C# Problem Overview


> Error : cannot implicitly convert type 'bool?' to 'bool'. An explicit > conversion exists (are you missing a cast?)

Code :

Test obj = new Test();
obj.IsDisplay = chkDisplay.IsChecked;

but when I use this method to cast the property into a bool then there is no error.

Test obj = new Test();
obj.IsDisplay = (bool) chkDisplay.IsChecked;

I would like to know why I need to cast this bool to bool?

C# Solutions


Solution 1 - C#

As the others stated bool? is not equal to bool. bool? can also be null, see Nullable<t> (msdn).

If you know what the null state wants to imply, you easily can use the ?? - null-coalescing operator (msdn) to convert your bool? to bool without any side effects (Exception).

Example:

//Let´s say "chkDisplay.IsChecked = null" has the same meaning as "chkDisplay.IsChecked = false" for you
//Let "check" be the value of "chkDisplay.IsChecked", unless "chkDisplay.IsChecked" is null, in which case "check = false"

bool check = chkDisplay.IsChecked ?? false;

Solution 2 - C#

You've declared IsChecked as a bool? (Nullable<bool>). A nullable boolean can be either true, false or null. Now ask yourself: If IsChecked was null, then what value should be assigned to IsDisplay (which can only take a true or false)? The answer is that there is no correct answer. An implicit cast here could only produce hidden trouble, which is why the designers decided to only allow an explicit conversion and not an implicit one.

Solution 3 - C#

I'm facing your question when I'm using the null check operator ?.:

if (!RolesList?.Any()) //Not accepted: cannot convert bool? to bool

So I'm using this instead

if (RolesList?.Any() != true)
  //value is null or false

In your case you should set it like so:

obj.IsVisible = chkDisplayStuff.IsChecked ?? false;

Solution 4 - C#

bool? is not a bool. It is in reality a Nullable<bool> http://msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx

If you need the bool value then you should either cast like you are doing or call the .Value property on the bool?. There is also a .HasValue property you can check to make sure that it is not null.

If IsChecked is null, this line will error.

obj.IsDisplay = (bool) chkDisplay.IsChecked;

Solution 5 - C#

bool is not equal to bool?.

bool can take two values, true and false.

bool? can take three, true, false, and null.

That is why they are different.

Solution 6 - C#

You can use below code

obj.IsDisplay = chkDisplay.IsChecked == true?true:false;

Solution 7 - C#

chkDisplay.IsChecked is of type bool?. Which means it can hold values true, false and null. However, obj.IsDisplay is of type bool. Which means it can only hold true or false.

Hence you have to explicitly cast it to type bool. However, this will still throw an exception if, the value you are trying to cast to bool is null.

bool? nullableBool = null;
bool notNullableBool = (bool)nullableBool; //This will throw InvalidOperationException

Solution 8 - C#

Try this

if (asset.IsUp ?? false)

Solution 9 - C#

cast your nullable value to value type

[HttpPost]
public ActionResult Index(bool? checkOffAge)
{
    if (checkOffAge != null) {
	   model.CheckOffAge =(bool)checkOffAge;
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnkit JainView Question on Stackoverflow
Solution 1 - C#Sigma BearView Answer on Stackoverflow
Solution 2 - C#Nikola DimitroffView Answer on Stackoverflow
Solution 3 - C#Shimmy WeitzhandlerView Answer on Stackoverflow
Solution 4 - C#TyCobbView Answer on Stackoverflow
Solution 5 - C#VladLView Answer on Stackoverflow
Solution 6 - C#MeteoricView Answer on Stackoverflow
Solution 7 - C#mridulaView Answer on Stackoverflow
Solution 8 - C#BobSpringView Answer on Stackoverflow
Solution 9 - C#RokiveView Answer on Stackoverflow