==> 인덱서.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]); }
    }
}

Posted by holland14
: