C# Default使用

在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T:

  • T 是引用类型还是值类型。

  • 如果 T 为值类型,则它是数值还是结构。

给定参数化类型 T 的一个变量 t,只有当 T 为引用类型时,语句 t = null 才有效;只有当 T 为数值类型而不是结构时,语句 t = 0 才能正常使用。 解决方案是使用 default 关键字,此关键字对于引用类型会返回 null,对于数值类型会返回零。 对于结构,此关键字将返回初始化为零或 null 的每个结构成员,具体取决于这些结构是值类型还是引用类型。 对于可以为 null 的值类型,默认返回 System.Nullable(Of T),它像任何结构一样初始化。

 

复制代码
namespace ConsoleApplication1
{
     class Program
    {
         static  void Main( string[] args)
        {
             //  Test with a non-empty list of integers.
            GenericList< int> gll =  new GenericList< int>();
            gll.AddNode( 5);
            gll.AddNode( 4);
            gll.AddNode( 3);
             int intVal = gll.GetLast();
             //  The following line displays 5.
            System.Console.WriteLine(intVal);

             //  Test with an empty list of integers.
            GenericList< int> gll2 =  new GenericList< int>();
            intVal = gll2.GetLast();
             //  The following line displays 0.
            System.Console.WriteLine(intVal);

             //  Test with a non-empty list of strings.
            GenericList< string> gll3 =  new GenericList< string>();
            gll3.AddNode( " five ");
            gll3.AddNode( " four ");
             string sVal = gll3.GetLast();
             //  The following line displays five.
            System.Console.WriteLine(sVal);

             //  Test with an empty list of strings.
            GenericList< string> gll4 =  new GenericList< string>();
            sVal = gll4.GetLast();
             //  The following line displays a blank line.
            System.Console.WriteLine(sVal);
        }
    }

     //  T is the type of data stored in a particular instance of GenericList.
     public  class GenericList<T>
    {
         private  class Node
        {
             //  Each node has a reference to the next node in the list.
             public Node Next;
             //  Each node holds a value of type T.
             public T Data;
        }

         //  The list is initially empty.
         private Node head =  null;

         //  Add a node at the beginning of the list with t as its data value.
         public  void AddNode(T t)
        {
            Node newNode =  new Node();
            newNode.Next = head;
            newNode.Data = t;
            head = newNode;
        }

         //  The following method returns the data value stored in the last node in
        
//  the list. If the list is empty, the default value for type T is
        
//  returned.
         public T GetLast()
        {
             //  The value of temp is returned as the value of the method. 
            
//  The following declaration initializes temp to the appropriate 
            
//  default value for type T. The default value is returned if the 
            
//  list is empty.
            T temp =  default(T);

            Node current = head;
             while (current !=  null)
            {
                temp = current.Data;
                current = current.Next;
            }
             return temp;
        }
    }
复制代码


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值