• C#学习——泛型(Generic)


    泛型(Generic)允许你延迟编写类或方法中的编程元素的数据类型规范,直到实际在应用程序中使用它的时候。换句话说,泛型允许您编写一个可以与任何数据类型一起工作的类或方法。

    你可以通过数据类型的替代参数编写类或方法的规范。当编译器遇到类的构造函数或方法的函数调用时,他会生成代码来处理指定的数据类型。

    泛型将类型参数的概念引入.NET,这使得可以设计类和方法将一种或多种类型的规范推迟到客户端代码声明和实例化该类或方法之前

    例如,通过使用类型参数T,你可以编写其它客户端代码可以使用的单个类,而不会产生运行时强制转换或装箱操作的成本或风险,如下所示:

    // Declare the generic class.
    public class GenericList<T>
    {
        public void Add(T input) { }
    }
    class TestGenericList
    {
        private class ExampleClass { }
        static void Main()
        {
            // Declare a list of type int.
            GenericList<int> list1 = new GenericList<int>();
            list1.Add(1);
    
            // Declare a list of type string.
            GenericList<string> list2 = new GenericList<string>();
            list2.Add("");
    
            // Declare a list of type ExampleClass.
            GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
            list3.Add(new ExampleClass());
        }
    }

    通用类型和方法结合了可重用性,类型安全和效率,这是非通用方类和方法无法实现的。泛型最常与集合及其操作方法一起使用。System.Collections.Generic命名空间包含了几个基于泛型的集合类。不推荐使用非通用集合(例如ArrayList),并处于兼容性目的对其进行维护。有关更多信息,请参见.NET中的泛型

      当然你可以创建自定义的通用类型和方法,以提供自己的通用解决方案和设计模式,这些类型和方法时类型安全且高效的。下面的代码演示了一个简单的通用链表类,以进行演示。(在大多数情况下,应该使用.NET提供的List类,而不是创建自己的类)。类型参数T用于多个位置,通常会使用具体的类型来指示列表(list)中存储的类型。它以下列方式使用:

      1、作为AddHead方法中方法参数的类型。

      2、作为嵌套Node类中Data属性的返回类型

      3、作为嵌套类中私有成员data的类型

    注意:嵌套的Node类可以使用T,注意当用具体类型实例化GenericList时例如GenericList<int>,每次出现T都将用int替换。

    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>类创建整数列表(list).即可轻松修改该以下代码以创建字符串列表或任何其它自定义类型:

      

    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");
        }
    }

    泛型概述:

      使用泛型类型可最大程度地提高代码重用性,类型安全性,和性能

      泛型最常见的用途是创建集合类

      .NET类库在System.Collections.Generic命名空间中包含几个通用集合类。应该尽可能使用他们,而不是System.Collections命名空间中ArrayList之类的类。

      你可以创建自己的通用接口(interface),类(class),方法(method),事件(event)和委托(delegates)。

      通用(泛型)类可能受到限制,以允许访问特定数据类型上的方法。

      可以在运行时通过反射获取有关通用(泛型)数据类型中使用的类型的信息。

  • 相关阅读:
    XCode快捷键 转
    [iOS] UIView的clipsTobounds属性
    ios 重用UI部分代码的好方法(再也不用为局部变量的命名而烦恼啦!)
    symbol(s) not found for architecture armv7
    duplicate symbol _main in: / linker command failed with exit code 1
    xcode4.3.2 arc模式下导入非arc的文件 转
    objective-c block 详解 转
    将asi-http-request引入到ARC工程需要做的 转
    浅用block 转
    在Xcode4.5中禁用ARC(Automatic Referencing Counting) 转
  • 原文地址:https://www.cnblogs.com/sy-liu/p/13087209.html
Copyright © 2020-2023  润新知