- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
如果我有一个方法需要一个参数,
Count
属性(property)这个参数的类型应该是什么?我会选择 IList<T>
在 .NET 4.5 之前,因为没有其他可索引的集合接口(interface)和数组实现它,这是一个很大的优势。
但是 .NET 4.5 引入了新的 IReadOnlyList<T>
接口(interface),我希望我的方法也支持它。我如何编写此方法以同时支持 IList<T>
和 IReadOnlyList<T>
又不违背DRY之类的基本原则?
编辑:丹尼尔的回答给了我一些想法:
public void Foo<T>(IList<T> list)
=> Foo(list, list.Count, (c, i) => c[i]);
public void Foo<T>(IReadOnlyList<T> list)
=> Foo(list, list.Count, (c, i) => c[i]);
private void Foo<TList, TItem>(
TList list, int count, Func<TList, int, TItem> indexer)
where TList : IEnumerable<TItem>
{
// Stuff
}
编辑 2: 或者我可以接受 IReadOnlyList<T>
并提供这样的助手:
public static class CollectionEx
{
public static IReadOnlyList<T> AsReadOnly<T>(this IList<T> list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
return list as IReadOnlyList<T> ?? new ReadOnlyWrapper<T>(list);
}
private sealed class ReadOnlyWrapper<T> : IReadOnlyList<T>
{
private readonly IList<T> _list;
public ReadOnlyWrapper(IList<T> list) => _list = list;
public int Count => _list.Count;
public T this[int index] => _list[index];
public IEnumerator<T> GetEnumerator() => _list.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
然后我可以这样调用它 Foo(list.AsReadOnly())
编辑 3:数组同时实现了 IList<T>
和 IReadOnlyList<T>
,List<T>
也是如此类(class)。这使得很难找到实现 IList<T>
的类。但不是 IReadOnlyList<T>
.
最佳答案
你在这里运气不好。 IList<T>
没有实现 IReadOnlyList<T>
. List<T>
确实实现了这两个接口(interface),但我认为这不是您想要的。
但是,您可以使用 LINQ:
Count()
扩展方法在内部检查实例实际上是否是一个集合,然后使用 Count
属性(property)。ElementAt()
扩展方法在内部检查实例是否实际上是一个列表,然后使用索引器。关于c# - IList<T> 和 IReadOnlyList<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12838122/
我正在尝试转换 IList>至 IList ,我的意思是有一个唯一的列表,其中包含第一个的所有元素(对象)。 public IList> PMTs { get
在 C# 中,如果我有一个 IList,但我不知道 IList 中对象的类型,我该如何创建一个 的副本>IList? 情况是这样的: 我有一个 CollectionEditor,它可以修改 IList
IList不继承 IList其中 IEnumerable继承IEnumerable . 如果out修饰符是大多数执行 IList 的唯一原因(例如 Collection , List )实现 ILis
尝试过: IList> matrix = new List()>(); 可是我做不到。我该怎么做?我需要一个字符串矩阵... 最佳答案 你需要: IList> matrix = new List>()
我有一个构建列表列表的方法。我想让返回类型使用通用 IList<> 接口(interface)来减少与下游具体 List<> 类型的耦合。但是,编译器在类型转换方面遇到了困难。 public ILis
我有一个 IList我想将此集合复制到 IList收藏 我试过这个: IList listAdminVAT = new AdministrationService(session).ListDecim
我有一个实现了 IList 的对象接口(interface),我想把它转换成IList或者 List ,我试过了 IList a=(IList)b; List a=(IList)b; IList a=
我似乎无法弄清楚为什么以下内容不起作用。 通过阅读,它似乎一定与 this 之类的东西有某种关联。 . public class Test { public void SomeFunction
我有几个类: class Vehicle { } class Car : Vehicle { } 我有一个派生类的列表: IList cars; 我想将列表转换为其基类,并尝试过: IList bas
List的定义在 .net 中显示它实现了各种接口(interface)。 public class List : IList, ICollection, IEnumerable, IList, IC
我想创建一个 ListCollectionView使用通用 IList但是构造函数得到一个非泛型 IList .如何将我的列表从 IList 转换为至 IList ? 如果有另一种方法可以创建 Lis
这个问题在这里已经有了答案: Remove items from one list in another (10 个答案) 关闭 8 年前。 很抱歉提出这样的基本问题,我是 LINQ 的新手,我正试
我正在实现 IListSource这需要一个方法 GetList()具有以下签名: IList GetList() 我正在使用 .NET Framework 2,我想返回一个实现 IList 的对象,
我有一个类(class)想要 IList , 但我有一个 Systems.Collection.IList , 来自 NHibernate 问题。 我想创建一个将其转换为 IList 的方法.我该怎么
我似乎找不到为我的 MVVM Xamarin 表单应用程序构建和使用数据的最佳实践或最佳机制。这可能是对如何解决问题的简单误解,但我不确定。 我曾尝试使用 IEnumerable、IList 和一个简
多年来,我们大部分时间都在使用泛型集合。有时我们确实需要任何东西的集合(好吧,通常只有一些不同的东西但没有公共(public)基类)。对于这种情况,我们可以使用 IList或通用 IList作为方法参
我有一个 IList目的。 Person 类有字段,FirstName , LastName等 我有一个函数需要 IList我想以与 IList 相同的顺序传入 FirstNames 列表目的。有没有
尝试这样转换 Products1 = (IList)basicProfile.Products2.Select(ToProductInfo) Products1 是一个 public IList Pr
我正在尝试创建一个列表列表,但在实例化列表时遇到了问题。 IList> allLists = List>(); 我遇到了这一行的编译错误。 最佳答案 你必须实例化一个 List的 IList , 不是
我有一个集合,它实现了一个扩展 IList 和 List 的接口(interface)。 public Interface IMySpecialCollection : IList, IList {
我是一名优秀的程序员,十分优秀!