인덱서의 정의 구문
[한정자] (인덱서의 반환 타입) this [인덱스 식별자]
예 :
public Object this [int index]
{
get
{
//데이터 반환
}
set
{
//데이터 저장
}
}
get또는 set만을 구현하여서, 읽기 또는 쓰기 전용으로 만들 수 있다.
indexer를 통해서 값을 가져와 사용하는 방법은, indexer의 클래스 인스턴스를 통해서 가져오기 때문이다.
(인덱서의 반환 타입) this [인덱스 식별자]
{
(필요에 따라서 get 또는 set을 구현, 둘 중 하나는 구현해야함)
}
예 :
public interface indexer
{
string this[int index]
{
get;
set;
}
}
public class test : indexer
{
private string[] arr = new string[10];
public string this[int index]
{
get { return arr[index]; }
set { arr[index] = value; }
}
}