C# 实现 IList 接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1176416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing IList interface
提问by Ashish Ashu
I am new to generics. I want to implement my own collection by deriving it from IList<T>
interface.
我是泛型的新手。我想通过从IList<T>
接口派生来实现我自己的集合。
Can you please provide me some link to a class that implements IList<T>
interface or provide me a code that at least implements Add
and Remove
methods?
您能否为我提供一些指向实现IList<T>
接口的类的链接,或者为我提供至少实现Add
和Remove
方法的代码?
采纳答案by Anton Gogolev
Unless you have a very compelling reason to do so, your best bet will be to inherit from System.Collections.ObjectModel.Collection<T>
since it has everything you need.
除非您有非常令人信服的理由这样做,否则您最好的选择是继承,System.Collections.ObjectModel.Collection<T>
因为它拥有您需要的一切。
Please note that although implementors of IList<T>
are not required to implement this[int]
(indexer) to be O(1) (basically, constant-time access), it's strongly recommended you do so.
请注意,虽然 的实现IList<T>
者不需要将this[int]
(索引器)实现为 O(1)(基本上,恒定时间访问),但强烈建议您这样做。
回答by TcKs
You can look at Mono project. There is available complete source codes, sou you can look how are some classes implemented. For example "System.Collections.Generics.List<T>".
你可以看看Mono 项目。有可用的完整源代码,所以你可以看看一些类是如何实现的。例如“System.Collections.Generics.List<T>”。
回答by chikak
In most cases you can simply use List<T>
or derive from List<T>
. If you derive from List<T>
you will automatically get the implementation for Add and Remove.
在大多数情况下,您可以简单地使用List<T>
或派生自List<T>
. 如果你从你派生,List<T>
你将自动获得添加和删除的实现。
回答by tranmq
In addition to deriving from List<T>
, you can facade List<T>
and add more features to your facade class.
除了派生自List<T>
,您还可以为List<T>
您的外观类添加更多功能。
class MyCollection<T> : IList<T>
{
private readonly IList<T> _list = new List<T>();
#region Implementation of IEnumerable
public IEnumerator<T> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Implementation of ICollection<T>
public void Add(T item)
{
_list.Add(item);
}
public void Clear()
{
_list.Clear();
}
public bool Contains(T item)
{
return _list.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_list.CopyTo(array, arrayIndex);
}
public bool Remove(T item)
{
return _list.Remove(item);
}
public int Count
{
get { return _list.Count; }
}
public bool IsReadOnly
{
get { return _list.IsReadOnly; }
}
#endregion
#region Implementation of IList<T>
public int IndexOf(T item)
{
return _list.IndexOf(item);
}
public void Insert(int index, T item)
{
_list.Insert(index, item);
}
public void RemoveAt(int index)
{
_list.RemoveAt(index);
}
public T this[int index]
{
get { return _list[index]; }
set { _list[index] = value; }
}
#endregion
#region Your Added Stuff
// Add new features to your collection.
#endregion
}
回答by Zotak
Inheriting from List is often the quickest approach but can be limiting later on down the line if you need to inherit from another class (e.g. ContextBoundObject etc.). It's pretty quick to implement IList and as pointed out above, it gives a lot more flexibility.
从 List 继承通常是最快的方法,但如果您需要从另一个类(例如 ContextBoundObject 等)继承,则可能会在以后进行限制。实现 IList 非常快,并且如上所述,它提供了更多的灵活性。
回答by marsh-wiggle
Visual Studio offers an automatic full working implementationof interfaces like IList<>.
Visual Studio 提供了IList<> 等接口的自动完整工作实现。
You need only to write this code (while the line
您只需要编写此代码(而行
readonly IList<T> _list = new List<T>();
is the important one!)
是最重要的!)
public class MyCollection<T> : IList<T>
{
// This line is important. Without it the auto implementation creates only
// methods with "NotImplemented" exceptions
readonly IList<T> _list = new List<T>();
}
Then click on the bulb symbol orplace the cursor on the IList<> and press Strg + "."You will become several implementations offered, like:
然后单击灯泡符号或将光标放在 IList<> 上并按Strg + "。" 您将成为提供的几种实现,例如: