我目前正在制作自己的基本 通用列表类(以更好地了解预定义的类的工作方式)。 我唯一遇到的问题是,我无法像在使用“System.Collections.Generic.List
”时那样访问数组内部的元素。
GenericList<type> list = new GenericList<type>();
list.Add(whatever);
这很好,但当尝试访问“whatever”时,我想能够编写:。
list[0];
但显然这并不起作用,因为在代码中我明显缺少了什么,我需要添加什么来完善我的通用类呢?
GenericList
类是什么样子的? - LukeHennerleypublic void ActOnElement<TP1>(int index, ActByRef<T,TP1> proc, ref TP1 param1) { proc(ref Array[index], ref TP param1); }
这样的方法,它将允许代码直接对列表项进行操作 [假设public delegate void ActByRef<T1,T2>(ref T1 p1, ref T2 p2);
]。如果有一个GenericList<Rectangle>
,这样的方法可以让代码说myList.ActOnItem(index, (ref Rectangle r, ref int v) => {r.X -= v; r.Width+=v;}, ref widthAdjust)
来更新一个列表项“就地”。 - supercat