• C# 2.0泛型初试


    using System.Collections.Generic;
    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;
            }
        }
    }
    class TestGenericList
    {
        static void Main()
        {
            // int is the type argument
            GenericList<string> list = new GenericList<string>();

            for (int x = 0; x < 10; x++)
            {
                list.AddHead(x.ToString()+"dsafl");
            }

            foreach (string i in list)
            {
                System.Console.Write(i + " ");
            }
            System.Console.WriteLine("\nDone");
        }
    }

  • 相关阅读:
    运行缓慢的查询语句
    EditPlus 替换所有文件夹下的文件包含子文件夹
    PRM–pageLoaded事件
    DataSet接收XML数据并按条件搜索
    复杂数据类型使用基础
    WebService客户端调用错误处理
    仿GOOGLE个性首页可移动图层效果
    asp.net 2.0中md5密码加密
    页面上的可鼠标移动内嵌页面层,并有关闭按钮,背景不可点击
    最好的。NET反编译工具
  • 原文地址:https://www.cnblogs.com/Oceanchip/p/265797.html
Copyright © 2020-2023  润新知