C#

48.C# - 정수형 인덱서와 문자열 인덱서

Godffs 2009. 8. 12. 20:40
반응형
정수형 인덱서와 문자열 인덱서를 배열에서 번호를 부여해서
사용하기 위한 방법입니다.


Program.cs
using System;

public class 정수형인덱서와문자열인덱서
{
    public static void Main(string[] args)
    {
        Record r = new Record();

        r.SetNum(1);
        r.SetData("홍길동");

        Console.WriteLine(r[1]);
        Console.WriteLine(r["name"]);
        Console.WriteLine(r.GetData(1));
    }
}

 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;
        data["num"] = num;
    }

    public void SetData(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]); }
    }
}

결과화면


48.IntIndex &StringIndex.zip
다운로드

반응형