52. 정수형인덱서와 문자열인덱서
.NET프로그래밍/C# 3.5 SP1 2009. 8. 13. 17:30 |==> 인덱서.cs
using System;
public class 인덱서
{
public static void Main()
{
//[1] 레코드 생성
Record r = new Record();
//[2] 데이터 저장
r.SetNum(1);
r.SetName("홍길동");
//[3] 데이터 출력
Console.WriteLine(r.GetData(1)); // 홍길동
Console.WriteLine(r[1]); // 홍길동
Console.WriteLine(r["Name"]); // 홍길동
}
}
==============================================================================================
==> Record.cs
using System;
using System.Collections;
public class Record
{
private int num;
private string name;
private Hashtable data = new Hashtable();
public void SetNum(int num)
{
this.num = num; // 1이 저장
data["Num"] = num;
}
public void SetName(string name)
{
this.name = name; // 홍길동
data["Name"] = name;
}
public string GetData(int index) // 메서드
{
if (index == 0)
{
return this.num.ToString();
}
else
{
return this.name;
}
}
public string this[int index] // 정수형인덱서
{
get { return GetData(index); }
}
public string this[string index] // 문자열인덱서
{
get { return Convert.ToString(data[index]); }
}
}
'.NET프로그래밍 > C# 3.5 SP1' 카테고리의 다른 글
알고리즘 - 11. 병합정렬(Merge) (0) | 2009.08.13 |
---|---|
알고리즘 - 10. 검색(Search) (0) | 2009.08.13 |
51. 인덱서(Indexer) (0) | 2009.08.13 |
50. 속성(Property) (0) | 2009.08.13 |
49. 메서드 오버로드(Method Overload) - 다중정의(=여러번 정의) (0) | 2009.08.13 |