인덱서[T]
간단한 예제
public void Run()
{
StringArray<string> strArr = new StringArray<string>();
strArr[4] = "Yangbeng! Good Boy";
Console.WriteLine(strArr[4]);
}
public class StringArray<T>
{
private T[] _arr = new T[10];
public T this[int index]
{
get => _arr[index];
set => _arr[index] = value;
}
}
결과
Yangbeng! Good Boy
심화
public void Run()
{
StringArray strArr = new StringArray();
Console.WriteLine(strArr["Hungry"]);
Console.WriteLine(strArr["Oh~ho"]);
}
public class StringArray
{
private string[] _strArr = new string[] { "Yangbeng", "Oh~ho", "Yang Yaho" };
public int Length => _strArr.Length;
public bool this[string str] => ExistStr(str);
private bool ExistStr(string str)
{
return _strArr.Any(f => f == str);
}
}
결과
False
True