zoukankan      html  css  js  c++  java
  • 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");
        }
    }

  • 相关阅读:
    Vue根据URL传参来控制全局 console.log 的开关
    原来你是这样的毛玻璃
    CSS3边框会动的信封
    判断当前系统当前浏览器是否安装启用 Adobe Flash Player,检查在chrome中的状态
    随笔一个正则
    PHP实现栈数据结构
    php实现一个单链表
    php中按值传递和按引用传递的一个问题
    利用shell脚本或者php移动某个文件夹下的文件到各自的日期组成的目录下
    php中DateTime、diff
  • 原文地址:https://www.cnblogs.com/Oceanchip/p/265797.html
Copyright © 2011-2022 走看看