ICollection vs ICollection <T> - The ambiguity between ICollection <T> .Count and ICollection.Count

Note. This is similar, but not quite the same as this other question.

I implemented the IBusinessCollection interface. It suffers from both ICollection<T> and the old unallocated ICollection . I would rather just reset the old busted ICollection , but I use WPF data binding with CollectionView, which wants me to implement a dead IList : - (

In any case, the interfaces look like this:

 public interface IBusinessCollection<T> : ICollection<T>, ICollection { } public interface ICollection<T> { int Count { get; } } public interface ICollection { int Count { get; } } 

Due to the use of Injection Dependency, I pass objects of type IBusinessCollection<T> using their interfaces and not specific types, so I have something like this:

 internal class AnonymousCollection : IBusinessCollection<string> { public int Count { get { return 5; } } } public class Factory { public static IBusinessCollection<string> Get() { return new AnonymousCollection(); } } 

When I try to call this code, I get an error message:

 var counter = Factory.Get(); counter.Count; // Won't compile // Ambiguity between 'ICollection<string>.Count' and 'ICollection.Count' 

There are three ways to make this compiler, but they are all ugly.

  • Enumerate the class for a specific implementation (which I don't know)

  • Explicit ICollection Class Expression

  • Explicit ICollection<T> Class Expression

Is there a fourth option that doesn't require me to drop things at all? I can make any changes I need so that IBusinessCollection<T>

+5
generics c # icollection
source share
2 answers

This seems to solve the problems in my quick tests.

 public interface IBusinessCollection<T> : ICollection<T>, ICollection { new int Count { get; } } 
+9
source share
 ICollection<string> counter = Factory.Get(); counter.Count; // No longer ambiguous 
-one
source share

All Articles