What is the difference between Nullable <bool> and bool?
When I redesign my classes, I get the following:
public Nullable<bool> Correct { get; set; } public Nullable<bool> Response { get; set; } I encoded:
public bool? Correct { get; set; } public bool? Response { get; set; } Can anyone tell me if there is a difference between the two. I have not seen a Nullable<bool> before, and I'm not sure why it is not just creating a "bool".
Note. I changed my code to bool? in response to John's comments
"A Nullable can be set to true false or null. The ability to assign null and boolean types is especially useful when dealing with databases and other data types that contain items that cannot be assigned. For example, a logical field in the database the data may hold true or false or may be undefined. "
Can anyone tell me if there is a difference between the two. I haven’t seen Nullable before, and I'm not sure why it doesn’t just create a “bool”
technically there is no difference between Nullable and bool ?. Whatever you write, they compile to Nullable in IL . so no difference.? it's just the c # compiler syntax.
why do we need a system for nullable
this is because it is used as type . And the type should be in namespace .
But is there a difference in bool and bool? . Since bool is a simple value type that cannot be assigned a null value, while you can assign a value to bool ?.
Nullable represents a value type to which null can be assigned, and it is in the System namespace.
Further, since it can be set to null, you can check if it has a value or not like it
if(Correct.HasValue) { //do some work } Yes there is a difference between Nullable<bool> and bool .
public Nullable<bool> Correct { get; set; } // can assign both true/false and null Correct = null; //possible then
in your case you cannot him
public bool Correct { get; set; } //can assign only true/false Correct = null; //not possible Perhaps the previous guy who is encoded cannot expose the bool? dataType.
System.Nullable<bool>equivalent tobool?
Update: No difference between Nullable<bool> and bool?
Nullable<bool> and bool? equivalent (the suffix "?" is syntactic sugar). Nullable<bool> means that in addition to the typical bool : true and false values, there is 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
A null value can be useful if you are working with undefined values, for example. in some cases, when you cannot determine whether the instance is correct or not, if any answer was issued; for example 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; }