实现List和ICollection [英] implementing List and ICollection

查看:94
本文介绍了实现List和ICollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的ObjectCollection类实现泛型.

有什么办法可以同时使用List< t>和和ICollection< t>在我的ObjectCollection类中.

实际上,Icollection仅向我提供Add,Insert,Remove ...一些可以为我的自定义实现覆盖的功能.我想使用List< t>中的更多功能喜欢排序...

基本上我希望它可以让我同时使用两个接口的功能.我们知道我们可以编写自己的自定义函数对Icollection中的对象进行排序,但是我知道List比ICollection具有更好的性能,因此我想同时使用这两个功能.

我有点困惑.

我会很感激你的.

I want to implement Generics for my ObjectCollection class.

Is there any way that I would be able to use both List<t> and ICollection<t> in my ObjectCollection class.

Actually Icollection Provide me only Add,Insert,Remove... some functions which I can override for my custom Implementation. I want to use some more fetures from List<t> like sort...

basically I want that it let me use the functionality of both the interfaces.we know that we can write our own custom function to sort the objects in the Icollection but I know the List gives better performace than ICollection so I want to use the both the features.

I am a little confused here.

I will appreaciate for your immed. help!!!

推荐答案

好吧IList<T>无论如何都会给您ICollection<T>,所以请使用它.对于Sort,也请使用IComparable<T>.
Well IList<T> will give you ICollection<T> anyway so use that. For Sort use IComparable<T> as well.
public class MyCollection<T> : IList<T>, IComparable<T>
{
    // ...
}




or

public class MyObject
{
    // ...
}
public class MyObjectCollection : IList<MyObject>, IComparable<MyObject>
{
    // ...
}


您是否看过System.Collections.Generic.IList<T>

它实现了ICollection< T> IEnumerable< T>.和IEnumerable.

它还有很多方法,例如OrderBy, ToList<T>, ToDictionary<T, TKey>等.
Have you looked at System.Collections.Generic.IList<T>

It implements ICollection<T>, IEnumerable<T> and IEnumerable.

It also has many, many methods such as OrderBy, ToList<T>, ToDictionary<T, TKey> etc.


这篇关于实现List和ICollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆