Nullable Type

- Nullable Types
System.Nullable<T> variable
Or
T? variable
Example:
               int? i = 10;
               double? d1 = 3.14;
               bool? flag = null;
               char? letter = 'a';
               int?[] arr = new int?[10];
2 common properties of Nullable Types:
1) HasValue 2) Value
Ex:
               int? x = 10;
               if (x.HasValue)
               {
                   System.Console.WriteLine(x.Value);
               }
               else
               {
                   System.Console.WriteLine("Undefined");
               }

Conversion / Casting:
Explicit Conversion:
               int? n = null;
               //int m1 = n;      // Will not compile.
               int m2 = (int)n;   // Compiles, but will create an exception if x is null.
               int m3 = n.Value;  // Compiles, but will create an exception if x is null.
Implicit Conversion:
               int? n1 = null;
               int? n2;
               n2 = 10;  // Implicit conversion.

Operators:
unary and binary operators :
Ex:
               int? a = 10;
               int? b = null;
               a++;         // Increment by 1, now a is 11.
               a = a * 10;  // Multiply by 10, now a is 110.
               a = a + b;   // Add b, now a is null.

Ex 2:
               int? num1 = 10;
               int? num2 = null;
               if (num1 >= num2)
               {
                   System.Console.WriteLine("num1 is greater than or equal to num2");
               }
               else
               {
                   // num1 is NOT less than num2
               }

Output: // num1 is NOT less than num2- When performing comparisons with nullable types, if one of the nullable types is null, the comparison is always evaluated to be false. It is therefore important not to assume that because a comparison is false, the opposite case is true.
- A comparison of two nullable types which are both null will be evaluated to true.

 ?? operator :
- defines a default value that is returned when a nullable type is assigned to a non-nullable type
Ex:
               int? c = null;
               // d = c, unless c is null, in which case d = -1.
               int d = c ?? -1;

- This operator can also be used with multiple nullable types
               int? e = null;
               int? f = null;
               // g = e or f, unless e and f are both null, in which case g = -1.
               int g = e ?? f ?? -1;

- cannot be used in conditionals such as with if, for, or while
               bool? b = null;
               if (b) // Error CS0266. -- Error: InvalidOperationException 
               {
               }
& and | operator (AND , OR operator)
               bool? x;
               bool? y;
X y x&y x|y
                        True  true       Truetrue
                        True  false       Falsetrue
                        True  null       Nulltrue
                        False  true       Falsetrue
                        False  false       Falsefalse
                        False  null       Falsenull
                        Null  true       Nulltrue
                        Null  false       Falsenull
                        Null  null       Nullnull

No comments:

Post a Comment