为啥要引入可空类型?

在数据库中,字段是可以为null值的,那么在C#中为了方便的操作数据库的值,微软引入了可空类型。

声明可空类型

我们可以使用两种方法声明一个可空类型:

1 Nullable<int> i = null;
2 int? i = null;

第二行是第一行的简写方法,其中“?”是微软为可空类型提供的一个语法糖。

我们看看可空类型的实现:

  1 // Type: System.Nullable`1
  2 // Assembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  3 // MVID: 255ABCDF-D9D6-4E3D-BAD4-F74D4CE3D7A8
  4 // Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll
  5 
  6 using System.Runtime;
  7 
  8 namespace System
  9 {
 10   /// <summary>
 11   /// 表示基础类型为值类型的对象,值类型与引用类型一样也可以分配 null。
 12   /// </summary>
 13   /// <typeparam name="T"><see cref="T:System.Nullable`1"/> 泛型类型的基础值类型。</typeparam><filterpriority>1</filterpriority>
 14   [Serializable]
 15   public struct Nullable<T> where T : struct
 16   {
 17     /// <summary>
 18     ///<see cref="T:System.Nullable`1"/> 结构的新实例初始化为指定值。
 19     /// </summary>
 20     /// <param name="value">一个值类型。</param>
 21     [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
 22     public Nullable(T value);
 23     /// <summary>
 24     /// 创建一个新的 <see cref="T:System.Nullable`1"/> 对象,并将其初始化为指定的值。
 25     /// </summary>
 26     /// 
 27     /// <returns>
 28     /// 一个 <see cref="T:System.Nullable`1"/> 对象,其 <see cref="P:System.Nullable`1.Value"/> 属性使用 <paramref name="value"/> 参数进行初始化。
 29     /// </returns>
 30     /// <param name="value">一个值类型。</param>
 31     public static implicit operator T?(T value);
 32     /// <summary>
 33     /// 返回指定的 <see cref="T:System.Nullable`1"/> 的值。
 34     /// </summary>
 35     /// 
 36     /// <returns>
 37     /// <paramref name="value"/> 参数的 <see cref="P:System.Nullable`1.Value"/> 属性的值。
 38     /// </returns>
 39     /// <param name="value">一个 <see cref="T:System.Nullable`1"/> 值。</param>
 40     public static explicit operator T(T? value);
 41     /// <summary>
 42     /// 检索当前 <see cref="T:System.Nullable`1"/> 对象的值,或该对象的默认值。
 43     /// </summary>
 44     /// 
 45     /// <returns>
 46     /// 如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 true,则为 <see cref="P:System.Nullable`1.Value"/> 属性的值;否则为当前 <see cref="T:System.Nullable`1"/> 对象的默认值。 默认值的类型为当前 <see cref="T:System.Nullable`1"/> 对象的类型参数,而默认值的值中只包含二进制零。
 47     /// </returns>
 48     [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
 49     public T GetValueOrDefault();
 50     /// <summary>
 51     /// 检索当前 <see cref="T:System.Nullable`1"/> 对象的值或指定的默认值。
 52     /// </summary>
 53     /// 
 54     /// <returns>
 55     /// 如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 true,则为 <see cref="P:System.Nullable`1.Value"/> 属性的值;否则为 <paramref name="defaultValue"/> 参数。
 56     /// </returns>
 57     /// <param name="defaultValue">如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 false,则为一个返回值。</param>
 58     public T GetValueOrDefault(T defaultValue);
 59     /// <summary>
 60     /// 指示当前 <see cref="T:System.Nullable`1"/> 对象是否等于指定的对象。
 61     /// </summary>
 62     /// 
 63     /// <returns>
 64     /// 如果 <paramref name="other"/> 等于当前的 <see cref="T:System.Nullable`1"/> 对象,则为 true;否则为 false。 此表描述如何定义所比较值的相等性: 返回值 说明 true <see cref="P:System.Nullable`1.HasValue"/> 属性为 false,并且 <paramref name="other"/> 参数为 null。 即,根据定义,两个 null 值相等。 - 或 - <see cref="P:System.Nullable`1.HasValue"/> 属性为 true,并且 <see cref="P:System.Nullable`1.Value"/> 属性返回的值等于 <paramref name="other"/> 参数。 false 当前 <see cref="T:System.Nullable`1"/> 结构的 <see cref="P:System.Nullable`1.HasValue"/> 属性为 true,并且 <paramref name="other"/> 参数为 null。 - 或 - 当前 <see cref="T:System.Nullable`1"/> 结构的 <see cref="P:System.Nullable`1.HasValue"/> 属性为 false,并且 <paramref name="other"/> 参数不为 null。 - 或 - 当前 <see cref="T:System.Nullable`1"/> 结构的 <see cref="P:System.Nullable`1.HasValue"/> 属性为 true,并且 <see cref="P:System.Nullable`1.Value"/> 属性返回的值不等于 <paramref name="other"/> 参数。
 65     /// </returns>
 66     /// <param name="other">一个对象。</param><filterpriority>1</filterpriority>
 67     public override bool Equals(object other);
 68     /// <summary>
 69     /// 检索由 <see cref="P:System.Nullable`1.Value"/> 属性返回的对象的哈希代码。
 70     /// </summary>
 71     /// 
 72     /// <returns>
 73     /// 如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 true,则为 <see cref="P:System.Nullable`1.Value"/> 属性返回的对象的哈希代码;如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 false,则为零。
 74     /// </returns>
 75     /// <filterpriority>1</filterpriority>
 76     public override int GetHashCode();
 77     /// <summary>
 78     /// 返回当前 <see cref="T:System.Nullable`1"/> 对象的值的文本表示形式。
 79     /// </summary>
 80     /// 
 81     /// <returns>
 82     /// 如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 true,则是当前 <see cref="T:System.Nullable`1"/> 对象的值的文本表示形式;如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 false,则是一个空字符串 ("")。
 83     /// </returns>
 84     /// <filterpriority>1</filterpriority>
 85     public override string ToString();
 86     /// <summary>
 87     /// 获取一个值,指示当前的 <see cref="T:System.Nullable`1"/> 对象是否有值。
 88     /// </summary>
 89     /// 
 90     /// <returns>
 91     /// 如果当前的 <see cref="T:System.Nullable`1"/> 对象具有值,则为 true;如果当前的 <see cref="T:System.Nullable`1"/> 对象没有值,则为 false。
 92     /// </returns>
 93     public bool HasValue { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; }
 94     /// <summary>
 95     /// 获取当前的 <see cref="T:System.Nullable`1"/> 值。
 96     /// </summary>
 97     /// 
 98     /// <returns>
 99     /// 如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 true,则为当前 <see cref="T:System.Nullable`1"/> 对象的值。 如果 <see cref="P:System.Nullable`1.HasValue"/> 属性为 false,则将引发异常。
100     /// </returns>
101     /// <exception cref="T:System.InvalidOperationException"><see cref="P:System.Nullable`1.HasValue"/> 属性为 false。</exception>
102     public T Value { [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")] get; }
103   }
104 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-20
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2021-07-18
猜你喜欢
  • 2022-12-23
  • 2022-02-08
  • 2022-12-23
  • 2021-10-07
  • 2022-03-07
  • 2021-11-26
相关资源
相似解决方案