值类型变量不能为null
#region 4-1使用Nullable<T>的各个成员 Nullable<int> x = 5;//包装等于5的值 x = new Nullable<int>(5); Console.WriteLine("Instance with value:"); Display(x); x = new Nullable<int>();//构造没有值的实例 Console.WriteLine("Instance with value:"); Display(x); #endregion #region 4-1 static void Display(Nullable<int> x)//显示诊断结果 { Console.WriteLine("HasValue:{0}", x.HasValue);// 如果当前的 System.Nullable<T> 对象具有值,则为 true;如果当前的 System.Nullable<T> 对象没有值,则为false。 if (x.HasValue)//只有有值是才执行,避免异常 { Console.WriteLine("Value:{0}", x.Value);//如果 System.Nullable<T>.HasValue 属性为 true,则为当前 System.Nullable<T> 对象的值。 如果System.Nullable<T>.HasValue 属性为 false,则将引发异常。 Console.WriteLine("Explicit conversion:{0}", (int)x);//显示转换 } Console.WriteLine("GetValueOrDefalt():{0}", x.GetValueOrDefault());// 如果 System.Nullable<T>.HasValue 属性为 true,则为 System.Nullable<T>.Value 属性的值;否则为当前System.Nullable<T> 对象的默认值。 Console.WriteLine("GetValueOrDefalt(10):{0}", x.GetValueOrDefault(10));// 如果 System.Nullable<T>.HasValue 属性为 true,则为 System.Nullable<T>.Value 属性的值;否则为defaultValue 参数。 Console.WriteLine("ToString():\"{0}\"", x.ToString());// 如果 System.Nullable<T>.HasValue 属性为 true,则是当前 System.Nullable<T> 对象的值的文本表示形式;如果System.Nullable<T>.HasValue 属性为 false,则是一个空字符串 ("")。 Console.WriteLine("GetHashCode():\"{0}\"", x.GetHashCode());// 如果 System.Nullable<T>.HasValue 属性为 true,则为 System.Nullable<T>.Value 属性返回的对象的哈希代码;如果System.Nullable<T>.HasValue 属性为 false,则为零。 Console.WriteLine(); } #endregion#region 4-2可空类型的装箱和拆箱行为 Nullable<int> nullable = 5; object boxed = nullable;//装箱成“有值的可空类型实例” Console.WriteLine(boxed.GetType()); int normal = (int)boxed;//拆箱成非可空变量 Console.WriteLine(normal); nullable = (Nullable<int>)boxed;//拆箱成可控变量 Console.WriteLine(nullable); nullable = new Nullable<int>(); boxed = nullable;//装箱成没有值的可空类型实例 Console.WriteLine(boxed == null); nullable = (Nullable<int>)boxed;//拆箱成可空变量 Console.WriteLine(nullable.HasValue); #endregion #region 4-3使用?修饰符 int? nullable = 5; object boxed = nullable; Console.WriteLine(boxed.GetType()); int normal = (int)boxed; Console.WriteLine(normal); nullable = (int?)boxed; Console.WriteLine(nullable); nullable = new int?(); boxed = nullable; Console.WriteLine(boxed == null); nullable = (int?)boxed; Console.WriteLine(nullable.HasValue); #endregion#region 4-4 class Person { DateTime birth; DateTime? death; string name; public TimeSpan Age { get { if (death == null)//检查HasValue IL: if (!this.death.HasValue) { return DateTime.Now - birth;//IL: return (TimeSpan) (DateTime.Now - this.birth); } else { return death.Value - birth;//拆包进行计算 IL:return (this.death.Value - this.birth); } } } public Person(string name, DateTime birth, DateTime? death) { this.birth = birth; this.death = death; this.name = name; } } #endregion #region 4-4Person Person turing = new Person("Alan Turing", new DateTime(1912, 6, 23), new DateTime(1954, 6, 7)); Person knuth = new Person("Donald Knuth", new DateTime(1938, 1, 10), null); Console.WriteLine("turing age:{0}", turing.Age.Days); Console.WriteLine("knuth age:{0}", knuth.Age); #endregion #region 对可空类型使用as PrintValueAsInt32(5); PrintValueAsInt32("some"); #endregion #region 对可空类型使用as static void PrintValueAsInt32(object o) { int? nullable = o as int?; Console.WriteLine(nullable.HasValue ? nullable.ToString() : "null"); Console.WriteLine(nullable.ToString()); } #endregion //使用可空操作符?? public TimeSpan Age { get { return (death ?? DateTime.Now) - birth; } } #region Tryxxx模式的备选实现 int?其他用法 int? parsed = TryParse("12121"); if (parsed != null) { Console.WriteLine("Parsed to {0}", parsed.Value); } else { Console.WriteLine("Couldn‘t parse"); } #endregion #region 4-5 static int? TryParse(string text) { int ret; if (int.TryParse(text, out ret)) { return ret; } else { return null; } } #endregion #region 4-6 部分比较 int?使用方法 public static class PartialComparer { public static int? Compare<T>(T first, T second) { return Compare(Comparer<T>.Default, first, second); } public static int? Compare<T>(Comparer<T> comparer, T first, T second) { int ret =comparer.Compare(first,second); return ret==0?new int?():ret; } public static int? ReferenceCompare<T>(T first, T second) where T : class { return first == second ? 0 : first == null ? -1 : second == null ? 1 : new int?(); } } #endregion原文:http://www.cnblogs.com/Tan-sir/p/5169203.html
