7

I am taking a web service written in VB and rewriting it in C#. The service class implements two interfaces that have four methods each. Three of the four methods have the same signatures method name and parameter list. The 4th method has the same name, but a different parameter list.

In VB, you explicitly identify the interface methods associated with the public service class methods. So for the methods that are the same, implementation looks like this:

class WebServiceClass
{
   Public Function Method1(result as Int32) As String Implements Interface1.Method1, Interface2.Method1

   Public Sub Method2(Id as Int64, P3 as Int32) Implements Interface1.Method2

   Public Sub Method3(In as Int64) Implements Interface2.Method2
}

Can you do this in C#?

I know you can explicitly define the methods in the web service class with Interface.Method2(Id as Int64, ...) and Interface2.Method2(In as...). But that will mean changing the names of these methods and consequently any application that uses these methods will have to be updated.

I could also change the names of the interface methods to match the web service methods but any application that uses these interfaces will have to be changed.

Is there any any to explicitly identify the interface method in the web service class, but keeping the web service class methods the same name and signature as the original?

1 Answer 1

9

Unfortunately there is simply no direct equivalent to this VB.NET feature in C#. There is no way in C# to implement an interface method and give it a different name. This can be simulated though by just creating the name you want and having the interface implementation forward to that method name

class WebServiceClass : Interface1, Interface2
{
  public string Method1(int result) { ... }
  public void Method2(long id, int p3) { ... }
  public void Method3(long in) { ... }

  string Interface1.Method1(int result) { return Method1(result); }
  void Interface1.Method2(long id, int p3) { Method2(id, p3); }
  string Interface2.Method1(int result) { return Method1(result); }
  void Interface2.Method2(long in) { Method3(in); }
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is an excellent pattern; explicit interface implementations that contain significant amounts of code are often an anti-pattern in inheritable classes, since derived classes can't wrap them. Having an explicit interface implementation simply chain to a protected virtual method makes things much cleaner.
Shouldn't Method1, Method2 and Method3 be private?
@GloriaSantin depends on your situation and whether you want them to be accessible to other instances
@supercat Derived classes can wrap inherited explicit interface implementations by casting base to the interface type, then invoking them. Or do you mean override instead of "wrap" because explicit interface implementations cannot be marked as virtual?
@Dai: If a derived class implements an interface, casting base to the interface type and invoking it would invoke the derived class implementation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.