zoukankan      html  css  js  c++  java
  • Nullable Value Type

          在.net2.0以前,值类型是不充许赋值为null的,这也是值类型和引用类型的一个重要区别。随着.net2.0的出现,这一局面得以改善,这种功能有点像数据库中的数据类型(int,datetime等),在数据库中列都可以设置是否可为Null。C#实现这种功能主要是依靠一个重要的结构:Nullable<T>,我们先看下它的实现:

    [Serializable]
        
    public struct Nullable<T> where T : struct
        {
            
    //
            
    // 摘要:
            
    //     Initializes a new instance of the System.Nullable<T> structure to the specified
            
    //     value.
            
    //
            
    // 参数:
            
    //   value:
            
    //     A value type.
            public Nullable(T value);

            
    public static explicit operator T(T? value);
            
    public static implicit operator T?(T value);

            
    // 摘要:
            
    //     Gets a value indicating whether the current System.Nullable<T> object has
            
    //     a value.
            
    //
            
    // 返回结果:
            
    //     true if the current System.Nullable<T> object has a value; false if the current
            
    //     System.Nullable<T> object has no value.
            public bool HasValue { get; }
            
    //
            
    // 摘要:
            
    //     Gets the value of the current System.Nullable<T> value.
            
    //
            
    // 返回结果:
            
    //     The value of the current System.Nullable<T> object if the System.Nullable<T>.HasValue
            
    //     property is true. An exception is thrown if the System.Nullable<T>.HasValue
            
    //     property is false.
            
    //
            
    // 异常:
            
    //   System.InvalidOperationException:
            
    //     The System.Nullable<T>.HasValue property is false.
            public T Value { get; }

            
    // 摘要:
            
    //     Indicates whether the current System.Nullable<T> object is equal to a specified
            
    //     object.
            
    //
            
    // 参数:
            
    //   other:
            
    //     An object.
            
    //
            
    // 返回结果:
            
    //     true if the other parameter is equal to the current System.Nullable<T> object;
            
    //     otherwise, false. This table describes how equality is defined for the compared
            
    //     values: Return Value Description true The System.Nullable<T>.HasValue property
            
    //     is false, and the other parameter is null. That is, two null values are equal
            
    //     by definition.  -or- The System.Nullable<T>.HasValue property is true, and
            
    //     the value returned by the System.Nullable<T>.Value property is equal to the
            
    //     other parameter.  false The System.Nullable<T>.HasValue property for the
            
    //     current System.Nullable<T> structure is true, and the other parameter is
            
    //     null.  -or- The System.Nullable<T>.HasValue property for the current System.Nullable<T>
            
    //     structure is false, and the other parameter is not null.  -or- The System.Nullable<T>.HasValue
            
    //     property for the current System.Nullable<T> structure is true, and the value
            
    //     returned by the System.Nullable<T>.Value property is not equal to the other
            
    //     parameter.
            public override bool Equals(object other);
            
    //
            
    // 摘要:
            
    //     Retrieves the hash code of the object returned by the System.Nullable<T>.Value
            
    //     property.
            
    //
            
    // 返回结果:
            
    //     The hash code of the object returned by the System.Nullable<T>.Value property
            
    //     if the System.Nullable<T>.HasValue property is true, or zero if the System.Nullable<T>.HasValue
            
    //     property is false.
            public override int GetHashCode();
            
    //
            
    // 摘要:
            
    //     Retrieves the value of the current System.Nullable<T> object, or the object's
            
    //     default value.
            
    //
            
    // 返回结果:
            
    //     The value of the System.Nullable<T>.Value property if the System.Nullable<T>.HasValue
            
    //     property is true; otherwise, the default value of the current System.Nullable<T>
            
    //     object. The type of the default value is the type argument of the current
            
    //     System.Nullable<T> object, and the value of the default value consists solely
            
    //     of binary zeroes.
            public T GetValueOrDefault();
            
    //
            
    // 摘要:
            
    //     Retrieves the value of the current System.Nullable<T> object, or the specified
            
    //     default value.
            
    //
            
    // 参数:
            
    //   defaultValue:
            
    //     A value to return if the System.Nullable<T>.HasValue property is false.
            
    //
            
    // 返回结果:
            
    //     The value of the System.Nullable<T>.Value property if the System.Nullable<T>.HasValue
            
    //     property is true; otherwise, the defaultValue parameter.
            public T GetValueOrDefault(T defaultValue);
            
    //
            
    // 摘要:
            
    //     Returns the text representation of the value of the current System.Nullable<T>
            
    //     object.
            
    //
            
    // 返回结果:
            
    //     The text representation of the value of the current System.Nullable<T> object
            
    //     if the System.Nullable<T>.HasValue property is true, or an empty string ("")
            
    //     if the System.Nullable<T>.HasValue property is false.
            public override string ToString();

      

           Nullable<T>数据类型:Nullable<T>能够使C#中的值类型赋值为null,但同时它本身是一个值类型。这个结构的实例比较小,在存储上依然可以存储在堆栈上。        

                Nullable<int> i = 1;
                Nullable
    <int> j = null;

          生成的IL代码足以说明Nullable<T>本身是值类型:

    .locals init ([0] valuetype [mscorlib]System.Nullable`1<int32> i,
    [
    1] valuetype [mscorlib]System.Nullable`1<int32> j)

      

          Nullable<T>的取值,既然能够充许值类型为null,那么如何读取值呢?

          第一:Nullable<T>属性
                1:HasValue:获取一个值,指示当前的 Nullable<T> 对象是否有值。
                2:Value:获取当前的 Nullable<T> 值。如果hasValue等于false,则会抛出异常。

          第二:Nullable<T>中包含两个特别重要的方法:
                1:T GetValueOrDefault();检索当前 Nullable<T> 对象的值,或该对象的默认值。internal T value=default(T),这个私有字段可以保证返回一个相应类型的默认值。
                2:T GetValueOrDefault(T defaultValue);检索当前 Nullable<T> 对象的值或指定的默认值。

         Nullable<T>的别名:Nullable<int> 可以写成int?,这是为了方便书写等原因设计。下面的代码是等价的:
       

    int? i=3;//Nullable<int> i=3
    int?j=null;//Nullable<int> j=null

      

          Nullable<T>对操作符的影响:

               1:一元操作符,如果操作数null,则结果返回null;
               2:二元操作符,如果其中一个操作符为null,结果返回null
               3:叛等:
                 1>如果两个操作数都为null,返回true。
                 2>如果有一个操作数为null,返回false。
                 3>如果两个操作数都不为null,则比较两个操作数的Value。
              4:比较操作符:
                1>如果其中一个操作数为null,则返回false。
                2>如果两个都不为null,则比较两个操作数的Value。

          C#对Nullable<T>的应用:??操作符:如果 ?? 运算符的左操作数非 null,该运算符将返回左操作数,否则返回右操作数。它结合了判断对象是否为空的操作。下面的代码是等价的:可以看出代码简洁不少。
     

    DateTime  k = j ?? DateTime.Now ;
    DateTime kk
    =j==null ?DateTime .Now :(DateTime )j ;

      

          Nullable Value Type的GetType方法,一个类型申明成Nullable<T>,我们理所当然的认为这个变量的类型会是Nullable<T>,但实际并非我们想象的那样。这里并不会返回Nullable(Int32),而是会返回System.Int32。CLR会直接返回T的类型,而不是Nullable<T>。

    Int32? i=1;
    i.GetType();

      

          Nullable Value Type对接口方法的影响:我们来看下Nullable<int> 和int在调用IComparable的区别。Nullable<int>并没有实现IComparable接口,但int有实现,所以我样想调用ToCompareTo方法可以这样写(CLR)对Nullable<T>有特殊处理:
       

    Int32? i = 1;
    Int32  iResult 
    = ((IComparable)i).CompareTo(1);

      

          如果没有CLR对Nullable<T>的特殊支持,我们需要这样写,明显可以看出是比较麻烦的方法:
      

     Int32 iResult2 = ((IComparable)(Int32)i).CompareTo(1);

       
      
     

  • 相关阅读:
    NJU 操作系统实验三
    Oracle数据库的下载安装和配置
    实模式/保护模式
    FAT12
    FAT12 img tool
    Mysql InnoDB 数据更新/删除导致锁表
    程序员,你总要有点自己的想法吧!
    支付系统安全设计思维导图
    从实时清分手续费记账看代码的易读性
    (7/8)借助枚举说一下数据类型定义规范
  • 原文地址:https://www.cnblogs.com/ASPNET2008/p/1506366.html
Copyright © 2011-2022 走看看