// 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;
            }

            // 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 next;
            public Node Next
            {
                get { return next; }
                set { next = 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;
        }

        /// <summary>
        /// foreach 문을 사용하기 위해 GetEnumerator 정의
        /// </summary>
        /// <returns></returns>
        public IEnumerator<T> GetEnumerator()
        {
            Node current = head;

            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 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("\nDone");

        }
    }

 

http://msdn.microsoft.com/en-us/library/0x6a29h6(VS.80).aspx

Local MSDN URL : ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.ko/dv_csref/html/7825ccb7-300d-4a82-88c4-629eadcd2741.htm

 

 

이 글은 스프링노트에서 작성되었습니다.

'닷넷 프레임워크' 카테고리의 다른 글

1. 프로세스의 형태  (0) 2009.04.27
Managed Code 개요  (0) 2009.04.27
IFormatProvider 인터페이스  (0) 2009.04.27
error MSB3323: 대처법  (0) 2009.04.27
C# 엑셀 파일 출력  (0) 2009.04.27

+ Recent posts