0

Though this question is specific to IQueryable design, but i came across many class and interface design where class or interface definition explicitly defines both IChildInterface and IParentInterface though IChildInterface : IParentInterface Like :

class MyClass : IChildInterface, IParentInterface

Question 1 : Under what circumstances is this required ?

Question 2 : If extension method with same name is defined for IChildInterface and IParentInterface then which one compiler will resolve to ?

rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44
  • Question1, maybe some do it for clarity for potential readers / code maintenance afterwards. Resharper will mark such things as unneeded, ofcourse you need to implement it still (it's just an interface definition) on the implementation. For Question 2, neither, if the compiler cannot find out what the type should be based on the available evidence it will ask you to be more specific, either by calling explicitely the StaticClasss.StaticMethod or to remove potential namespaces which are blocking. Having a same name could be counter productive though, it would imply similar behavior – Icepickle Feb 10 '18 at 08:39
  • `IEnumerable` does not *implement* `IEnumerable`. Interfaces implement nothing. It *requires* that its implementers implement `IEnumerable`. – Eric Lippert Feb 11 '18 at 02:04
  • Don't ask two questions in one question. As you've seen what happens is the question gets closed and your second question never gets answered. I note that you could answer your second question yourself by **trying it** or by **reading the specification**. – Eric Lippert Feb 11 '18 at 02:05
  • Eric : I used wrong term _implements_ rather than _inherits_. Corrected it. Thanks !! – rahulaga-msft Feb 11 '18 at 03:27

1 Answers1

0

Eric Lippert wrote about your first question in his blog (with the title "So many interfaces!"): https://blogs.msdn.microsoft.com/ericlippert/2011/04/04/so-many-interfaces/

He basically says that it is not required to add an interface and its base type but mentions:

Perhaps because they believe that doing so makes the code easier to understand and more self-documenting.

For extension methods the method in the closest enclosing namespace is chosen or as the C# language specification states about "Extension method invocations":

Starting with the closest enclosing namespace declaration, continuing with each enclosing namespace declaration, and ending with the containing compilation unit

Oli
  • 157
  • 4
  • 1
    The method with the more derived interface is *usually* chosen. What do you think happens with this program? `using N.M; class B {} class D : B {} namespace N { static class X { public static void M(this B b) { System.Console.WriteLine("B"); } } namespace M { static class X { public static void M(this D d) { System.Console.WriteLine("D"); } } } public class P { public static void Main() { new D().M(); } } }` – Eric Lippert Feb 11 '18 at 02:19
  • 1
    Plainly both extension methods are available; why is the one that takes a B chosen instead of the one that takes a D? – Eric Lippert Feb 11 '18 at 02:20
  • @EricLippert : If am not wrong, this is because of "_extension method definition not allowed in nested class_" rule. – rahulaga-msft Feb 11 '18 at 03:46
  • Nope, there's no nested class there. – Eric Lippert Feb 11 '18 at 19:15
  • @EricLippert: Thanks, you're obviously right. The extension method in the closest enclosing namespace is chosen. I edited my answer and added the according sentence from the language specification. – Oli Feb 11 '18 at 21:33