[英]generic list class in c#
我目前正在制作我自己的非常基本的通用列表類 (以便更好地理解預定義的列表類如何工作)。 我遇到的唯一問題是我無法像通常使用“ System.Collections.Generic.List
”那樣訪問數組中的元素。
GenericList<type> list = new GenericList<type>();
list.Add(whatever);
這工作正常,但在嘗試訪問我希望能夠編寫的“任何”時:
list[0];
但這顯然不起作用,因為我在代碼中明顯遺漏了一些內容,我需要添加到我完全正常工作的泛型類中的是什么?
它被稱為索引器 ,如下所示:
public T this[int i]
{
get
{
return array[i];
}
set
{
array[i] = value;
}
}
我認為您需要做的就是實現IList<T>
,以獲得所有基本功能
public interface IList<T>
{
int IndexOf(T item);
void Insert(int index, T item);
void RemoveAt(int index);
T this[int index] { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.