• 泛型概述


    泛型类和泛型方法兼复用性、类型安全和高效率于一身,是与之对应的非泛型的类和方法所不及。泛型广泛用于容器(collections)和对容器操作的方法中。.NET框架2.0的类库提供一个新的命名空间System.Collections.Generic,其中包含了一些新的基于泛型的容器类。要查找新的泛型容器类(collection classes)的示例代码,请参见基础类库中的泛型。当然,你也可以创建自己的泛型类和方法,以提供你自己的泛化的方案和设计模式,这是类型安全且高效的。下面的示例代码以一个简单的泛型链表类作为示范。(多数情况下,推荐使用由.NET框架类库提供的List<T>类,而不是创建自己的表。)类型参数T在多处使用,具体类型通常在这些地方来指明表中元素的类型。类型参数T有以下几种用法:

    1.        在AddHead方法中,作为方法参数的类型。

    2.        在公共方法GetNext中,以及嵌套类Node的 Data属性中作为返回值的类型。

    3.        在嵌套类中,作为私有成员data的类型。

    注意一点,T对嵌套的类Node也是有效的。当用一个具体类来实现MyList<T>时——如MyList<int>——每个出现过的T都要用int代替。

    using System;
    using System.Collections.Generic;
     
    public class MyList<T> //type parameter T in angle brackets
        {
            private Node head;
    // The nested type is also generic on T.
            private class Node          
            {
                private Node next;
    //T as private member data type:
                private T data;         
    //T used in non-generic constructor:
                public Node(T t)        
                {
                    next = null;
                    data = t;
                }
                public Node Next
                {
                    get { return next; }
                    set { next = value; }
                }
    //T as return type of property:
                public T Data           
                {
                    get { return data; }
                    set { data = value; }
                }
            }
            public MyList()
            {
                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;
                }
            }
        }

    下面的示例代码演示了客户代码如何使用泛型类MyList<T>,来创建一个整数表。通过简单地改变参数的类型,很容易改写下面的代码,以创建字符串或其他自定义类型的表。

    class Program
        {
            static void Main(string[] args)
            {
    //int is the type argument.
               MyList<int> list = new MyList<int>();
                for (int x = 0; x < 10; x++)
                    list.AddHead(x);
     
                foreach (int i in list)
                {
                    Console.WriteLine(i);
                }
                Console.WriteLine("Done");
            }
        }
  • 相关阅读:
    C# using的三种用法
    C# 匿名方法和Lambda表达式
    c#中内置委托
    c#委托中的匿名方法和lambda表达式
    java生成条形码
    根据日期筛选和模糊查询
    mysql中ifnull()方法的用法
    mysql数据库中的出发器
    动态SQL之<where>、<if>条件判断
    动态sql
  • 原文地址:https://www.cnblogs.com/XiaoRuLiang/p/12422163.html
Copyright © 2020-2023  润新知