C#

72.C# - 리스트 제네릭 클래스 ( List Generic Class)

Godffs 2009. 8. 18. 00:29
반응형

네임스페이스인 System.Collections.Generic; 를 선언하게 되면 리스트 제네릭 을 사용할 수 있습니다. 리스트 제네릭 클래스는 어떠한 자료 형태도 다 받아 드릴 수 있어 코드의 간결화로 개발자 입장에서는 유용한 클래스 입니다.
사용-예)ArrayList를 대신해서 사용


Program.cs
using System;
using System.Collections.Generic;

public class 리스트제네릭클래스
{
    static void Main()
    {
        //정수형
        List<int> su = new List<int>();
        su.Add(10); su.Add(20); su.Add(30);

        for (int i = 0; i < su.Count; i++) {
            Console.WriteLine("{0}",su[i]);
        }

        //문자열
        List<string> str = new List<string>();
        str.Add("안녕"); str.Add("반가워"); str.Add("또 봐");
        for (int i = 0; i < str.Count; i++) {
            Console.WriteLine("{0}",str[i]);
        }

        //개체형(테이블형태) : 문자열, 정수형
        List<ProductInfo> lstProduct = new List<ProductInfo>();

        ProductInfo pi1 = new ProductInfo(); //속성사용
        pi1.MedelName = "TV"; pi1.Quantity = 10;
        lstProduct.Add(pi1);
       
        //ProductInfo pi2 = new ProductInfo();//계선 전
       lstProduct.Add( new ProductInfo("RADIO", 5)
                            ); //계선후
//생성자 사용 (추천)
                                                                        
        /*계선전
        ProductInfo pi3 = new ProductInfo();
        pi3.MedelName = "DVD"; pi3.Quantity = 3;
        */
        //컬렉션(개체) 초기화자
       lstProduct.Add(new ProductInfo()
       { MedelName="DV",Quantity=3  }
        ); //계선후


        /*계선전
        lstProduct.Add(pi1);
        lstProduct.Add(pi2);
        lstProduct.Add(pi3);
        */

        //출력 : 윈폼/웹폼에서는 DataSouce 개념 적용
        for (int i = 0; i < lstProduct.Count; i++)
        {
            Console.WriteLine("{0}, {1}",lstProduct[i].MedelName,
               lstProduct[i].Quantity);

        }
    }
}

결과화면


72.ListGeneric.zip
다운로드

반응형