모든 개체에 add an indexer 수 있습니다. 예를 들어 (MSDN에서 직접) :
class SampleCollection<T>
{
// Declare an array to store the data elements.
private T[] arr = new T[100];
// Define the indexer, which will allow client code
// to use [] notation on the class instance itself.
// (See line 2 of code in Main below.)
public T this[int i]
{
get
{
// This indexer is very simple, and just returns or sets
// the corresponding element from the internal array.
return arr[i];
}
set
{
arr[i] = value;
}
}
}
개체는 내부적으로 100 개의 요소 배열을 관리합니다. 귀하의 경우에는 3 가지 요소 만 사용하면됩니다. 해당 객체의 사용은 당신이 찾고있는 무엇을 닮은 것 : 인덱서가 명시 적으로 예에 int
으로 정의된다
// Declare an instance of the SampleCollection type.
SampleCollection<string> stringCollection = new SampleCollection<string>();
// Use [] notation on the type.
stringCollection[0] = "Hello, World";
System.Console.WriteLine(stringCollection[0]);
참고도있다. 인덱서에는 다른 유형도 사용할 수 있습니다. (string
은 일반적인 대안입니다.)
왜 이렇게하고 싶습니까? List 제네릭 유형을 사용한 적이 있습니까? 또한 인덱서를 사용하여 요소에 액세스 할 수 있습니다. –
모든 개체에 인덱서를 추가 할 수 있습니다. http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx – David
@david에서 설명한 것처럼 색인 생성기'myArray' http://www.c-sharpcorner.com/uploadfile/vivek4u_swamy/indexers-and-properties/ – geedubb