C# nullable type (Nullable)

C# single question mark? and double question mark??

? : The single question mark is used to assign null to int, double, bool and other data types that cannot be directly assigned to null, which means that the data type is Nullable.

int? i = 3;

Equivalent to:

Nullable<int> i = new Nullable<int>(3);
int i; //default value 0
int? ii; //default value is null

?? : Double question marks can be used to judge that a variable returns a specified value when it is null.

Next we explain in detail.

C# nullable type (Nullable)

C# provides a special data type, the nullable  type (nullable type), which can represent values ​​within the normal range of its underlying value type, plus a null value.

For example, Nullable<Int32>, pronounced "nullable Int32", can be assigned any value between -2,147,483,648 and 2,147,483,647, and can also be assigned the null value. Similarly, Nullable<bool> variables can be assigned true or false or null.

The ability to assign null to numeric or Boolean types is particularly useful when 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 value true or false, or it can be undefined.

The syntax for declaring a  nullable  type (nullable type) is as follows:

< data_type> ? <variable_name> = null;

The following example demonstrates the use of nullable data types:

example

 
  using 
  System
  ;
   
  namespace CalculatorApplication
   
  {
      
  class NullablesAtShow
      
  {
         
  static 
  void Main
  (
  string
  [
  ] args
  )
         
  {
            
  int
  ? num1
  = 
  null
  ;
            
  int
  ? num2 
  =
  45
  ;
            
  double
  ? number3
  =
  new 
  double
  ?
  (
  )
  ;
            
  double
  ? num4 
  =
  3.14157
  ;
            
            
  bool
  ? boolval 
  =
  new
  bool
  ?
  (
  )
  ;
   
            
  // Display value
            
            Console
  .
  WriteLine
  (
  "Show values ​​of nullable types: {0}, {1}, {2}, {3}",
                               num1, num2, num3, num4
  )
  ;
            Console
  .
  WriteLine
  (
  "A nullable boolean: {0}", boolval
  )
  ;
            Console
  .
  ReadLine
  (
  )
  ;
   
         
  }
      
  }
   
  }
   
 

When the above code is compiled and executed, it produces the following result:

Show values ​​of nullable types: , 45, , 3.14157
A nullable boolean:

Null coalescing operator ( ?? )

The Null coalescing operator is used to define default values ​​for nullable and reference types. The Null coalescing operator defines a default for type conversions in case a nullable type is Null. The null coalescing operator implicitly converts the operand type to the type of the operand of another nullable (or non-nullable) value type.

If the value of the first operand is null, the operator returns the value of the second operand, otherwise it returns the value of the first operand. The following example demonstrates this:

example

 
  using 
  System
  ;
   
  namespace CalculatorApplication
   
  {
      
  class NullablesAtShow
      
  {
            
         
  static 
  void Main
  (
  string
  [
  ] args
  )
         
  {
            
            
  double
  ? num1
  =
  null
  ;
            
  double
  ? num2
  =
  3.14157
  ;
            
  double num3
  ;
            number3
  = num1
  ?? 
  5.34
  ;      
  // num1 returns 5.34 if it is null
            Console
  .
  WriteLine
  (
  "Value of num3: {0}", num3
  )
  ;
            number3
  = num2 
  ??
  5.34
  ;
            Console
  .
  WriteLine
  (
  "Value of num3: {0}", num3
  )
  ;
            Console
  .
  ReadLine
  (
  )
  ;
   
         
  }
      
  }
   
  }
   
 

When the above code is compiled and executed, it produces the following result:

Value of num3: 5.34
Value of num3: 3.14157

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/131930635