List Generic Class 란 ?
인덱스로 엑세스 할수 있는 강력한 형식의 개체 목록을 나타냅니다.
목록의 검색, 정렬 및 조작에 사용할 수 있는 메서드를 제공 하고 있습니다.
ArrayList 클래스의 제네릭 형태에 따라, 클래스는 크기가 동적으로 증가하는 배열을
사용하여 IList 제네릭 인터페이스를 구현하고 있습니다.
Program.cs
using System;
using System.Collections.Generic;
public class 리스트제네릭{
public 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 = "TV"; pi3.Quantity = 3;
lstProduct.Add(new ProductInfo()
{ MedelName = "DVD", Quantity = 3 });
//출력 : 윈폼/웹폼에서는 DataSouce 개념 적용
for (int i = 0; i < lstProduct.Count; i++)
{
Console.WriteLine("{0},{1}", lstProduct[i].MedelName,lstProduct
[i].Quantity);
}
}
}
ProductInfo.cs |
using System;
public class ProductInfo
{
//상품명
public string MedelName { get; set; }
//판매량
public int Quantity { get; set; }
//생성자
public ProductInfo(string modelName, int quantity)
{
this.MedelName = modelName;
this.Quantity = quantity;
}
//매개변수 없는 생성자
public ProductInfo()
{
//Empty
}
}
1. 속성사용하는 방법)
//상품명
ProductInfo pi1 = new ProductInfo(); //속성사용
pi1.MedelName = "TV"; pi1.Quantity = 10;
lstProduct.Add(pi1);
public string MedelName { get; set; }
//판매량
public int Quantity { get; set; }
2. 생성자사용하는 방법)
ProductInfo pi2 = new ProductInfo(); //생성자
lstProduct.Add(new ProductInfo
("RADIO", 5));
3. 컬렉션/개체 초기화자
더 많은 사항이 궁금하시면..
ProductInfo pi3 = new ProductInfo();
pi3.MedelName = "TV"; pi3.Quantity = 3;
lstProduct.Add(new ProductInfo()
{ MedelName = "DVD", Quantity = 3 });
아래의 링크를 들어가서 제네릭 클래스에 대해 알아보시면 됩니다...
http://msdn.microsoft.com/ko-kr/library/6sh2ey19(VS.85).aspx
'.Net Project > .Net C#' 카테고리의 다른 글
94장 형식매개변수에 대한 제약조건 (0) | 2009.08.18 |
---|---|
93장 제네릭클래스(Generic Class) (0) | 2009.08.18 |
91장 예외처리(Exception) (0) | 2009.08.17 |
90장 연산자오버로딩 (0) | 2009.08.17 |
89장 변환연산자 (0) | 2009.08.17 |