zoukankan      html  css  js  c++  java
  • c# T obj = default(T);

    泛型类和泛型方法同时具备可重用性、类型安全和效率,这是非泛型类和非泛型方法无法具备的。泛型通常用在集合和在集合上运行的方法中。.NET Framework 2.0 版类库提供一个新的命名空间 System.Collections.Generic,其中包含几个新的基于泛型的集合类。建议面向 2.0 版的所有应用程序都使用新的泛型集合类,而不要使用旧的非泛型集合类,如 ArrayList。有关更多信息,请参见 .NET Framework 类库中的泛型(C# 编程指南)

    当然,也可以创建自定义泛型类型和方法,以提供自己的通用解决方案,设计类型安全的高效模式。下面的代码示例演示一个用于演示用途的简单泛型链接列表类。(大多数情况下,建议使用 .NET Framework 类库提供的 List<T> 类,而不要自行创建类。)在通常使用具体类型来指示列表中所存储项的类型时,可使用类型参数 T。其使用方法如下:

    • 在 AddHead 方法中作为方法参数的类型。

    • 在 Node 嵌套类中作为公共方法 GetNext 和 Data 属性的返回类型。

    • 在嵌套类中作为私有成员数据的类型。

    注意,T 可用于 Node 嵌套类。如果使用具体类型实例化 GenericList<T>(例如,作为 GenericList<int>),则所有的 T 都将被替换为 int。

     
    // type parameter T in angle brackets
    public class GenericList<T> 
    {
        // The nested class is also generic on T
        private class Node
        {
            // T used in non-generic constructor
            public Node(T t)
            {
                next = null;
                data = t;
            }
    
            private Node next;
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
            
            // T as private member data type
            private T data;
    
            // T as return type of property
            public T Data  
            {
                get { return data; }
                set { data = value; }
            }
        }
    
        private Node head;
        
        // constructor
        public GenericList() 
        {
            head = null;
        }
    
        // T as method parameter type:
        public void AddHead(T t) 
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }
    
        public IEnumerator<T> GetEnumerator()
        {
            Node current = head;
    
            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }
    }
    
    

    下面的代码示例演示客户端代码如何使用泛型 GenericList<T> 类来创建整数列表。只需更改类型参数,即可方便地修改下面的代码示例,创建字符串或任何其他自定义类型的列表:

     
    class TestGenericList
    {
        static void Main()
        {
            // int is the type argument
            GenericList<int> list = new GenericList<int>();
    
            for (int x = 0; x < 10; x++)
            {
                list.AddHead(x);
            }
    
            foreach (int i in list)
            {
                System.Console.Write(i + " ");
            }
            System.Console.WriteLine("
    Done");
        }
    }
    

    在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 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;
            }
        }
    复制代码

  • 相关阅读:
    两个页面相同js方法兼容
    两个页面js方法兼容
    jQuery Chosen 使用
    我写的一个抓取基金净值的工具
    自己做的一个固定大小对象内存池,效率大概为原始的new/delete的2倍
    用libevent实现的echo服务器及telnet客户端
    共享一个防止脚本重复启动的shell脚本
    神煞排盘软件
    一个在字符串中查找多个关键字的函数strstrs(三种不同算法实现及效率分析)
    写了一个时间处理的类,能将人类时间转换成距离公元零年一月一日秒数(时间戳),同时支持时间戳转换成日期时间
  • 原文地址:https://www.cnblogs.com/liuqiyun/p/6728736.html
Copyright © 2011-2022 走看看