c# - What's the difference between "bool" and "bool?"? -


I use the "bool" type for the variable because I was used in C ++, and I used the function I try to keep values ​​or qualities I hope to be bullion in my variable, though I am often facing such cases where the result is "bool?" And not "boole" and the underlying casting fails.

What is the difference between the two and when is each used? Also, should I use "boole"? As for my variables? Is this the best practice?

After a type of ? is the symbol bool? For a shortcut Nullable & lt; Bool & gt; Allows wrapping types, and is able to assign them null

bool? can have three different values: true , false and null .

In addition to this, there are no short-circuiting operators (& amp;) defined for bool

only logical and, inclusive or Operators are defined and they behave like this:

  xyx & amp; Y X | Y true true true true true true true true true false true truth tap empty real false false false false false false false false false false false false meaningless true false tap false false falsiferous null futile nullable type original The form is a general structure, in which the following public properties are:  
  public configuration is worthwhile & lt; T & gt; Where T: Straight {Get Public Hair Smile; } Public T Value {Received; }}  

HasValue property indicates that the current object is the value, and the value property will get the current value or if the object is If it is false, then it will throw an invalid operation exception.

Now you should think about something, a tap is a straight, a value type that can not be zero, so why is the following statement valid?

  int? A = Null;  

This example will compile in:

  .locals init (value-type [mscorlib] System.Nullable`1  V_0) IL_0000: ldloca S V_0 IL_0002: Initb Joulightime [mscorlib] System.Nullable`1 & lt; Int32 & gt;  

A call, which initializes each field of type value in a specific address to an empty reference or a proper primitive type 0.

That's it, what's going on here is the default.

  int? A = Null; Is equivalent to  

:

  faucetable  Int & gt; A = new taps qualified & lt; Int & gt; ();  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -