当我有一个ICollection< T>的变量时在C#中,我无法将其传递给期望IReadOnlyCollection< T>的函数:
public void Foo() { ICollection<int> data = new List<int>(); // Bar(data); // Not allowed: Cannot implicitly cast ICollection<int> to IReadOnlyCollection<int> Bar(data.ToList()); // Works,since List<T> implements IReadOnlyCollection<T> } public void Bar(IReadOnlyCollection<int> data) { if (data.Count == 1) { /* ... */ } // ... }
显然问题在于ICollection< T>不从IReadOnlyCollection< T>继承- 但为什么?的ICollection< T>应该是IReadOnlyCollection< T>的全功能集合.加上修改集合的功能.
传递参数的最佳解决方案是什么?
一方面,因为我不想改变Bar中的集合,只需要计数并迭代集合,我想要一个IReadOnlyCollection.
解决方法@H_301_14@
没有标准的解决方案AFAIK,但是这样做并不难
public static class MyExtensions
{
public static IReadOnlyCollection<T> AsReadOnly<T>(this ICollection<T> source)
{
if (source == null) throw new ArgumentNullException("source");
return source as IReadOnlyCollection<T> ?? new ReadOnlyCollectionAdapter<T>(source);
}
sealed class ReadOnlyCollectionAdapter<T> : IReadOnlyCollection<T>
{
readonly ICollection<T> source;
public ReadOnlyCollectionAdapter(ICollection<T> source) => this.source = source;
public int Count => source.Count;
public IEnumerator<T> GetEnumerator() => source.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
然后按如下方式使用它
Bar(data.AsReadOnly());
public static class MyExtensions { public static IReadOnlyCollection<T> AsReadOnly<T>(this ICollection<T> source) { if (source == null) throw new ArgumentNullException("source"); return source as IReadOnlyCollection<T> ?? new ReadOnlyCollectionAdapter<T>(source); } sealed class ReadOnlyCollectionAdapter<T> : IReadOnlyCollection<T> { readonly ICollection<T> source; public ReadOnlyCollectionAdapter(ICollection<T> source) => this.source = source; public int Count => source.Count; public IEnumerator<T> GetEnumerator() => source.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } }
然后按如下方式使用它
Bar(data.AsReadOnly());