class NullableExample { staticvoid Main() { int? num =null; if (num.HasValue ==true) { System.Console.WriteLine("num = "+ num.Value); } else { System.Console.WriteLine("num = Null"); } //y is set to zeroint y = num.GetValueOrDefault(); // num.Value throws an InvalidOperationException if num.HasValue is falsetry try { y = num.Value; } catch (System.InvalidOperationException e) { System.Console.WriteLine(e.Message); } } }
使用 ?? 运算符分配默认值,当前值为空的可空类型被赋值给非空类型时将应用该默认值,如 int? x = null; int y = x ?? -1;。
不允许使用嵌套的可空类型。将不编译下面一行:Nullable<Nullable<int>> n;
例子1:
//Copyright (C) Microsoft Corporation. All rights reserved. using System; class NullableBasics ...{ staticvoid DisplayValue(int? num) ...{ if (num.HasValue ==true) ...{ Console.WriteLine("num = "+ num); } else ...{ Console.WriteLine("num = null"); } // num.Value throws an InvalidOperationException if num.HasValue is false try ...{ Console.WriteLine("value = {0}", num.Value); } catch (InvalidOperationException e) ...{ Console.WriteLine(e.Message); } } staticvoid Main() ...{ DisplayValue(1); DisplayValue(null); Console.Read(); } }
输出结果:
num =1 value =1 num =null 可为空的对象必须具有一个值。
例子2:
using System; class NullableBoxing { staticvoid Main() { int? a; object oa; // Assigns a to Nullable<int> (value = default(int), hasValue = false). a =null; // Assigns oa to null (because x==null), not boxed "int?". oa = a; Console.WriteLine("Testing 'a' and 'boxed a' for null..."); // Nullable variables can be compared to null. if (a ==null) { Console.WriteLine(" a == null"); } // Boxed nullable variables can be compared to null // because boxing a nullable where HasValue==false // sets the reference to null. if (oa ==null) ...{ Console.WriteLine(" oa == null"); } Console.WriteLine("Unboxing a nullable type..."); int? b =10; object ob = b; // Boxed nullable types can be unboxed int? unBoxedB = (int?)ob; Console.WriteLine(" b={0}, unBoxedB={0}", b, unBoxedB); // Unboxing a nullable type set to null works if // unboxed into a nullable type. int? unBoxedA = (int?)oa; if (oa ==null&& unBoxedA ==null) ...{ Console.WriteLine(" a and unBoxedA are null"); } Console.WriteLine("Attempting to unbox into non-nullable type..."); // Unboxing a nullable type set to null throws an // exception if unboxed into a non-nullable type. try ...{ int unBoxedA2 = (int)oa; } catch (Exception e) ...{ Console.WriteLine(" {0}", e.Message); } Console.Read(); } }
输出结果为:
Testing 'a' and 'boxed a'fornull... a ==null oa ==null Unboxing a nullable type... b=10, unBoxedB=10 a and unBoxedA are null Attempting to unbox into non-nullable type... 未将对象引用设置到对象的实例。
例子3:
using System; class NullableOperator ...{ staticint? GetNullableInt() ...{ returnnull; } staticstring GetStringValue() ...{ returnnull; } staticvoid Main() ...{ // ?? operator example. int? x =null; // y = x, unless x is null, in which case y = -1. int y = x ??-1; Console.WriteLine("y == "+ y); // Assign i to return value of method, unless // return value is null, in which case assign // default value of int to i. int i = GetNullableInt() ??default(int); Console.WriteLine("i == "+ i); // ?? also works with reference types. string s = GetStringValue(); // Display contents of s, unless s is null, // in which case display "Unspecified". Console.WriteLine("s = {0}", s ??"null"); Console.Read(); } }