Csharp/CSharp Tutorial/Data Type/Nullable

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

A nullable type

using System; 
 
class MainClass{ 
  public static void Main() { 
    int? count = null; 
 
    if(count.HasValue) 
      Console.WriteLine("count has this value: " + count.Value); 
    else  
      Console.WriteLine("count has no value"); 
 
    count = 100; 
 
    if(count.HasValue) 
      Console.WriteLine("count has this value: " + count.Value); 
    else  
      Console.WriteLine("count has no value");     
  } 
}
count has no value
count has this value: 100

Assign value to nullable int

using System; 
 
class MainClass { 
  public static void Main() { 
    int? count = null; 
    int? result = null; 
 
    int incr = 10; 
 
    count = 100; 
    result = count + incr; 
 
    if(result.HasValue) 
      Console.WriteLine("result has this value: " + result.Value); 
    else  
      Console.WriteLine("result has no value");     
 
  } 
}
result has this value: 110

Nullable long

using System;
public class Employee
{
    public Employee( string Name ) {
        this.firstName = firstName;
        this.terminationDate = null;
        this.ssn = default(Nullable<long>);
    }
    public string firstName;
    public Nullable<DateTime> terminationDate;
    public long? ssn; 
}
public class MainClass
{
    static void Main() {
        Employee emp = new Employee( "A");
        emp.ssn = 1234567890;
        Console.WriteLine( "{0} {1}", emp.firstName);
        if( emp.terminationDate.HasValue ) {
            Console.WriteLine( "Start Date: {0}", emp.terminationDate );
        }
        long tempSSN = emp.ssn ?? -1;
        Console.WriteLine( "SSN: {0}", tempSSN );
    }
}

Nullable Struct

using System;
struct Point                                    
{
   public int x;                                   
   public int y;                                   
   public Point(int xVal, int yVal)             
   {
      x = xVal; 
      y = yVal;
   }
}
class MainClass
{
   static void Main()
   {
      Point p = new Point(6, 11);     
      Point? nullablePoint  = new Point(5, 10);     
      Console.WriteLine("p.x: {0}", p.x);
      Console.WriteLine("p.y: {0}", p.y);
      Console.WriteLine("nullablePoint.x: {0}", nullablePoint.Value.x);
      Console.WriteLine("nullablePoint.y: {0}", nullablePoint.Value.y);
   }
}
p.x: 6
p.y: 11
nullablePoint.x: 5
nullablePoint.y: 10

Nullable Types Access: Explicitly use properties

using System;
class MainClass
{
   static void Main()
   {
      int? nullableInteger = 15;
     
      if (nullableInteger.HasValue)
         Console.WriteLine("{0}", nullableInteger.Value);
   }
}
15

Nullable Types Access: shortcut syntax

using System;
class MainClass
{
   static void Main()
   {
      int? nullableInteger = 15;
     
      if (nullableInteger != null)
         Console.WriteLine("{0}", nullableInteger);
   }
}
15

Nullable Types Assignment

using System;
class MainClass
{
   static void Main()
   {
      int? nullableInteger1, nullableInteger2, nullableInteger3;
      nullableInteger1 = 28;
      nullableInteger2 = nullableInteger1;
      nullableInteger3 = null;
      Console.WriteLine("nullableInteger1: {0}, nullableInteger2: {1}", nullableInteger1, nullableInteger2);
      Console.WriteLine("nullableInteger3 {0} null", nullableInteger3 == null ? "is" : "is not");
   }
}
nullableInteger1: 28, nullableInteger2: 28
nullableInteger3 is null

Null Coalescing Operator

using System;
class MainClass
{
   static void Main()
   {
      int? nullableInteger = null;
      Console.WriteLine("nullableInteger: {0}", nullableInteger ?? -1);
      nullableInteger = 10;
      Console.WriteLine("nullableInteger: {0}", nullableInteger ?? -1);
   }
}
nullableInteger: -1
nullableInteger: 10

null unification

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
public class MainClass
{
   public static void Main(){
        int? x1 = null;
        int? x2 = new int?();
        int? y1 = 55;
        int? y2 = new int?(55);
        Console.WriteLine("{0}...{1}...{2}...{3}", x1, x2, y1, y2);
        Console.WriteLine("x1 == null? {0}", x1 == null);
        object x1o = x1; 
        Console.WriteLine("x1o == null? {0}", x1o == null);
        Console.WriteLine("{0}...{1}", x1.GetType(), x1o.GetType());
   }
}
......55...55
x1 == null? True
x1o == null? True
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an ob
ject.
   at MainClass.Main()

Use nullable objects in expressions: result contains null, because one operand is null

using System; 
 
class MainClass { 
  public static void Main() { 
    int? count = null; 
    int? result = null; 
 
    int incr = 10; 
 
    result = count + incr; 
 
    if(result.HasValue) 
      Console.WriteLine("result has this value: " + result.Value); 
    else  
      Console.WriteLine("result has no value");     
 
  } 
}
result has no value

Using ??

using System; 
 
class MainClass { 
  static double myMethod() { 
      Console.WriteLine("In myMethod()."); 
      return 0.0; 
  } 
 
  public static void Main() { 
    double? defaultValue = 1.5; 
    double currentBalance; 
 
    currentBalance = defaultValue ?? myMethod(); 
    Console.WriteLine(currentBalance); 
    
    defaultValue = null; 
    currentBalance = defaultValue ?? myMethod(); 
    Console.WriteLine(currentBalance); 
    
  } 
}
1.5
In myMethod().
0