0

I have a sample piece of code with me:

bool _HasParsed;
object IsCheckedAsObj = GetCheckedStatus();
if (IsCheckedAsObj == null)
{
    throw new InvalidOperationException("Status not found");
}
_HasParsed = (bool?)IsCheckedAsObj;    //why (bool?) instead of (bool)

In the last line, I can understand that they are parsing the object to boolean. But what is that '?' doing there? Whats the difference between (bool?) instead of (bool)?

Sandy
  • 11,332
  • 27
  • 76
  • 122

6 Answers6

5

The type bool? is shorthand for Nullable<bool>.

The code doesn't compile as it stands. You will get the error message "Cannot implicitly convert type 'bool?' to 'bool'.".

If you declare the variable as nullable too, it will work:

bool? _HasParsed;

That might of course mean that you need to do other changes in the code. You can use _HasParsed.HasValue to check if the variable is not null, and use _HasParsed.Value to get the bool value.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • so what will be the result, if casting fails? – Sandy Feb 13 '12 at 12:54
  • @rapsalands I believe, if `IsCheckedAsObj` is either `null`, a `bool` or a `Nullable` (`bool?`) it will work. If `IsCheckAsObj` is none of these, casting will fail and an exception thrown. If this is undesired, try `_HasParsed = IsCheckedAsObj as bool?;` – Lukazoid Feb 13 '12 at 12:58
  • @rapsalands: If the casting fails, you will get the exception `InvalidCastException` at runtime. – Guffa Feb 13 '12 at 12:59
  • Thanks. I got the answer. SO asking me to accept the answer only after more 6 minutes – Sandy Feb 13 '12 at 13:00
4

bool? is shorthand for Nullable<bool>. Your code doesn't need it though, because there's a check against null before the assignment.

Edited: As Jeow Li Huan correctly stated in the comments, the check for null implies that the return value of GetCheckedStatus() is of type bool?. The following simplified snipped will do the same as the original code in OP's question:

object IsCheckedAsObj = GetCheckedStatus();
if (IsCheckedAsObj == null)
    throw new InvalidOperationException("Status not found");
bool _HasParsed = (bool)IsCheckedAsObj;
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • Interesting, didn't know you could directly cast a `bool?` to `bool`. Thought you had to use `.Value` (obviously fails if it's null at runtime, but I thought it would be compile error). I just checked it and it does work though. – Davy8 Feb 13 '12 at 13:03
  • Your edited version is less correct. If GetCheckedStatus() returns bool, if (IsCheckedAsObj == null) will always be false. So we should assume it returns bool?. The same code works and does not need calls to HasValue or Value – Jeow Li Huan Feb 13 '12 at 13:07
1

The difference between bool and bool? is that bool? is a Nullable<bool> which means that the bool has three possible values: false, true and null (= not set).

bool? nullableBool = null;

if ( b.HasValue )
{
   bool notNullableBool = b.Value;
}

Here is the MSDN documentation.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
1

Types with ?, such as bool? are nullable. Learn how to use them.
It also can be represented as Nullable<bool>. And can only be applied for struct types.
Nullable type's feature is that you can assign its value or null to them.

Using:

int? number;
// do something with number
if (number.HasValue)
{
  Console.WriteLine(number.Value);
}
else
{
  Console.WriteLine("No value");
}

or

Console.WriteLine(number.HasValue ? number.Value : "No value");
Sergey Gavruk
  • 3,538
  • 2
  • 20
  • 31
0

It means it's a boolean that can also be null.

See Nullable Types on MSDN.

ken2k
  • 48,145
  • 10
  • 116
  • 176
0

The ? is shorthand for the struct below:

struct Nullable<T> 
{ 
    public bool HasValue; 
    public T Value; 
} 

You can use this struct directly, but the ? is the shortcut syntax to make the resulting code much cleaner. Rather than typing: Nullable x = new Nullable(125);

Instead, you can write: int? x = 125;

This doesn't work with string, as a string is a Reference type and not a Value type.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188