4

When I reverse engineer my classes I get the following:

public Nullable<bool> Correct  { get; set; }
public Nullable<bool> Response { get; set; }

I coded:

public bool? Correct  { get; set; }
public bool? Response { get; set; }

Can someone tell me if there is any difference between these two. I have not seen the Nullable<bool> before and I'm not sure why it does not just create a "bool".

Note: I changed my coded to bool? in response to comments by Jon

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 1
    I suspect you actually coded `public bool? Correct { get; set; }`. The compiler doesn't convert `bool` into `bool?` automatically. – Jon Skeet Aug 01 '13 at 06:04
  • You sure you didn't write `bool?` instead of `bool`? – Robert Rouhani Aug 01 '13 at 06:04
  • I noticed that Nullable requires the System namespace but bool? does not. Anyone know what that is ? – Alan2 Aug 01 '13 at 06:11
  • If you use a different tool to reverse engineer your code, it might produce `bool?` back - this should be a hint to you that there's no difference - the reverse engineering tool cannot know which of the two you wrote. – Damien_The_Unbeliever Aug 01 '13 at 06:14
  • `bool?` is just an alias the C# compiler defined for your convenience. – CodesInChaos Aug 01 '13 at 06:19
  • Now it's really just http://stackoverflow.com/questions/56518/is-there-any-difference-between-bool-and-nullablebool?rq=1 – Jon Skeet Aug 01 '13 at 06:20

5 Answers5

5

"A Nullable can be assigned the values true false, or null. The ability to assign null to numeric and Boolean types is especially useful when you are dealing with databases and other data types that contain elements that may not be assigned a value. For example, a Boolean field in a database can store the values true or false, or it may be undefined."

Nullable Types

  • 3
    This doesn't explain what's going on in the OP's situation - which I suspect to be a misdiagnosis. – Jon Skeet Aug 01 '13 at 06:04
  • @Jon - Now I realize that the database allows NULLs and my code does not. The database scripts were done by our DBA and I coded the classes (incorrectly). I still wonder why the reverse engineer does not just specify bool? – Alan2 Aug 01 '13 at 06:07
  • Well, at least now you can also do sanity check on your DB. –  Aug 01 '13 at 06:08
  • I changed my code to use Nullable and it now asks me to include the System namespace. So are they really the same? – Alan2 Aug 01 '13 at 06:10
  • technically whatever you write they will compile down to Nullable in IL. so no difference. As far as why require system, it is because it is used as a type. And type needs to be in a namespace – Ehsan Aug 01 '13 at 06:14
  • Yes, as mentioned, they work the same with the difference that you can actually declare/assign it NULL. –  Aug 01 '13 at 06:14
  • 1
    @Alan `Nullable` and `bool?` is similar to `int` and `System.Int32`. They are just two ways of representing the same thing. You had to add the `System` namespace because you where using the longer version, just like you would need to add `System` if you used `Int32` instead of `int`. – Scott Chamberlain Aug 01 '13 at 07:03
5

Can someone tell me if there is any difference between these two. I have not seen the Nullable before and I'm not sure why it does not just create a "bool"

technically there is no difference in Nullable and bool?. Whatever you write they will compile down to Nullable in IL. so no difference. The ? is just C# compiler syntax.

why require system for Nullable

it is because it is used as a type. And type needs to be in a namespace.

But there is a difference in bool and bool?. As bool is a simple value type that cannot be assigned null value whereas you can assign value to bool?.

Nullable represents a value type that can be assigned null and it lies in the namespace System.

Further as it can be assigned null therefore you can check whether it has value or not like this

if(Correct.HasValue)
{
  //do some work
}
Ehsan
  • 31,833
  • 6
  • 56
  • 65
2

Yes there is difference between Nullable<bool> and bool.

public Nullable<bool> Correct { get; set; } // can assign both true/false and null
Correct = null;  //possible 

whereas

in your case you can't have it

public bool Correct { get; set; } //can assign only true/false
Correct = null;  //not possible

Maybe the previous guy who coded may not exposed to bool? dataType.

System.Nullable<bool> is equivalent to bool?

Update: There is no difference between Nullable<bool> and bool?

Praveen
  • 55,303
  • 33
  • 133
  • 164
2

Nullable<bool> and bool? are equivalent ("?" suffix is a syntactic sugar). Nullable<bool> means that in addition to typical bool values: true and false, there's a third value: null.

http://msdn.microsoft.com/en-us/library/1t3y8s4s(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx

Null value could be useful if you work with uncertain values, e.g. in some cases you can't tell if the instance is correct one or not, if any response has been given; for instance in your case

  // true  - instance is correct
  // false - instance is incorrect
  // null  - additional info required
  public bool? Correct { get; set; }
  // true  - response was given 
  // false - no response
  // null  - say, the response is in the process
  public bool? Response { get; set; }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

There is no difference.

Hint: Nullable<Nullable<bool>> n; // not allowed

Source msdn Nullable Types

WhileTrueSleep
  • 1,524
  • 1
  • 19
  • 32