Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 20 Interfacesprevious at this levelnext at this level 20.2 Interface membersprevious at this levelnext at this level 20.2.5 Interface member access Paragraph 11 Interface members are accessed through member access (§14.5.4) and indexer access (§14.5.6.2) expressions of the form I.M and I[A], where I is an instance of an interface type, M is a method, property, or event of that interface type, and A is an indexer argument list. Paragraph 21 For interfaces that are strictly single-inheritance (each interface in the inheritance chain has exactly zero or one direct base interface), the effects of the member lookup (§14.3), method invocation (§14.5.5.1), and indexer access (§14.5.6.2) rules are exactly the same as for classes and structs: More derived members hide less derived members with the same name or signature. 2 However, for multiple-inheritance interfaces, ambiguities can occur when two or more unrelated base interfaces declare members with the same name or signature. 3 This section shows several examples of such situations. 4 In all cases, explicit casts can be used to resolve the ambiguities. [Example: In the example
interface IList  
{  
   int Count { get; set; }  
}  
interface ICounter  
{  
   void Count(int i);  
}  
interface IListCounter: IList, ICounter {}  
class C  
{  
   void Test(IListCounter x) {  
      x.Count(1);      // Error  
      x.Count = 1;          // Error  
      ((IList)x).Count = 1;   // Ok, invokes IList.Count.set  
      ((ICounter)x).Count(1);  // Ok, invokes ICounter.Count  
   }  
}  
the first two statements cause compile-time errors because the member lookup (§14.3) of Count in IListCounter is ambiguous. As illustrated by the example, the ambiguity is resolved by casting x to the appropriate base interface type. Such casts have no run-time costs-they merely consist of viewing the instance as a less derived type at compile-time. end example]
[Example: In the example
interface IInteger  
{  
   void Add(int i);  
}  
interface IDouble  
{  
   void Add(double d);  
}  
interface INumber: IInteger, IDouble {}  
class C  
{  
   void Test(INumber n) {  
      n.Add(1);      // Error, both Add methods are applicable  
      n.Add(1.0);     // Ok, only IDouble.Add is applicable  
      ((IInteger)n).Add(1);  // Ok, only IInteger.Add is a candidate  
      ((IDouble)n).Add(1);   // Ok, only IDouble.Add is a candidate  
   }  
}  
the invocation n.Add(1) is ambiguous because a method invocation (§14.5.5.1) requires all overloaded candidate methods to be declared in the same type. However, the invocation n.Add(1.0) is permitted because only IDouble.Add is applicable. When explicit casts are inserted, there is only one candidate method, and thus no ambiguity. end example]
[Example: In the example
interface IBase  
{  
   void F(int i);  
}  
interface ILeft: IBase  
{  
   new void F(int i);  
}  
interface IRight: IBase  
{  
   void G();  
}  
interface IDerived: ILeft, IRight {}  
class A  
{  
   void Test(IDerived d) {  
      d.F(1);      // Invokes ILeft.F  
      ((IBase)d).F(1);   // Invokes IBase.F  
      ((ILeft)d).F(1);   // Invokes ILeft.F  
      ((IRight)d).F(1);  // Invokes IBase.F  
   }  
}  
the IBase.F member is hidden by the ILeft.F member. The invocation d.F(1) thus selects ILeft.F, even though IBase.F appears to not be hidden in the access path that leads through IRight.
The intuitive rule for hiding in multiple-inheritance interfaces is simply this: If a member is hidden in any access path, it is hidden in all access paths. Because the access path from IDerived to ILeft to IBase hides IBase.F, the member is also hidden in the access path from IDerived to IRight to IBase. end example]
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS