1

Visual studio tells me that I dont implement the methods if I dont declare them "explicitly" in terms of interface. Here are my methods:

    public interface IGetMenus
{
    List<Menu> GetMyMenus();
    void InsertMenu(string topic, string subTopic);
    void UpdateMainMenu(int menu_id, string topic);
    void UpdateSubMenu(int menu_id, string topic);
}

Here is an example of how visual studio wants me to implement the interface:

List<Menu> IGetMenus.GetMyMenus()
  {
  }

Why is that?

BlackFire27
  • 1,470
  • 5
  • 21
  • 32
  • Does your class already inherit a class or implement another interface (not `IGetMenus`) that has the same method declarations? – Yuriy Guts Feb 28 '12 at 14:04
  • 2
    but where's the class that implements the interface? – vulkanino Feb 28 '12 at 14:04
  • I didnt know that this questioned existed before..I know why to implement an interface explicitly.. but I dont have methods with the same names...I got it though..thanks – BlackFire27 Feb 28 '12 at 14:18

3 Answers3

4

Usually the reason is that you have a method with the same name + parameters but a different return type already.

In C# this is not valid, the only way to implement the interface in this case is to do it explicitly or move/rename the other method.

ntziolis
  • 10,091
  • 1
  • 34
  • 50
2

A good example is a method that you want to make sure the class wants to call. This is usually an expensive method that doesn't add needed functionality.

ObjectCache is a good example. You can add, delete, and check items with the implicit methods. However, Enumerator is defined explicitly.

foreach(var item in (ObjectCache as Enumerator))
{
     ...
}

So that you don't just enumerate the entire cache without explicitly doing so.

Joe
  • 80,724
  • 18
  • 127
  • 145
  • Very good example, will keep in in mind next time I'm asked why I would need explicit implementations, however in this case the issue seems that VS demands explicit implementation, which is usually do duplicate method signatures – ntziolis Feb 28 '12 at 14:19
0

An important thing to note when explicitly implementing members, btw, is that there is no way for a derived class to override an explicitly-implemented interface member in its parent class without making that member inaccessible and impossible to chain to (if a BaseClass explicitly implements IFoo.Foo, the only way for DerivedClass to override that is to implement IFoo.Foo itself; if the derived class does so, then IFoo.Foo will refer to the derived class' method, and there will be no way to access the IFoo.Foo implementation in the parent class). Consequently, if there may be any need for a derived-class member to chain to a parent-class implementation, it's often a good idea to put the "guts" of an interface-member implementation in a protected virtual member, and then explicitly implement the interface member so it simply calls the protected virtual one. This will allow derived classes to both override that member and chain to it.

supercat
  • 77,689
  • 9
  • 166
  • 211