bool? and other types with question marks

Posted Monday, July 24, 2006 10:40 AM by C-Dog's .NET Tip of the Day

At some point you might try to use a new object and Visual Studio reports a compiler error such as "Cannot implicitly convert type bool? to bool". This only occurs as you are working with new .NET 2.0 objects. The truth is bool? is actually the same thing as Nullable. It is just that VS2005 reports errors about nullable types using a question mark. I talked a long time ago about nullable types here.

Where I encountered this was I was trying to use the OpenFileDialog class in WPF and it uses a return type of Nullable. So in the past, the ShowDialog returned a type of bool, in this case it retunrs a type of Nullable. The key to remember with nullable types is that you have to check the Value property to get the actual value.

For example:

if (myOpenFileDialog.ShowDialog().Value)
    // do something

If you are worried about null values coming back be sure and check the IsNull property first before trying to access the value.

Read the complete post at