Friday, April 1, 2011

C# Fun and bugs - Multiple interface ambiguities

Error 1 Ambiguity between 'System.Collections.Generic.ICollection<double>.Count' and 'System.Collections.ICollection.Count'


// My interface 
 public interface ITest: IList<double>, ICollection { } 
 // The class public 
class bob : List<double>, ITest { } 
 // Using .Count from the ITest fails 
public class Test 
 { public Test() { ITest iTest = new bob(); int a = iTest.Count; } }

I googled this and check with a few "experts" and found nothing.  Then I looked at the error and found that IList and ICollection both define int Count.  So the compiler does not know which count is used in the interface.  The simple solution is to add:
   new int Count {get;} 
to ITest, this resolves the issue by providing one "Count" to the interface.


No comments:

Post a Comment