Question

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?

Was it helpful?

Solution

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); }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top