class SampleCollection<T>
{
// Declare an array to store the data elements.
private T[] arr = new T[100];
int nextIndex = 0;
// Define the indexer to allow client code to use [] notation.
public T this[int i] => arr[i];
public void Add(T value)
{
if (nextIndex >= arr.Length)
throw new IndexOutOfRangeException($"The collection can hold only {arr.Length} elements.");
arr[nextIndex++] = value;
}
}
- 클래스나 구조체의 인스턴스를 배열처럼 인덱싱할 수 있게 함.
- 해당 접근자가 매개 변수를 사용한다는 점을 제외하면 속성과 유사
링크 :
[인덱서 - C# 프로그래밍 가이드
C#의 인덱서를 사용하면 클래스 또는 구조체 인스턴스를 배열처럼 인덱싱할 수 있습니다. 형식 또는 인스턴스 멤버를 지정하지 않고 인덱싱된 값을 설정하거나 가져올 수 있습니다.
learn.microsoft.com](https://learn.microsoft.com/ko-kr/dotnet/csharp/programming-guide/indexers/)