Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent of VB.NET interface implementations

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?

like image 671
Gloria Santin Avatar asked Jan 24 '14 18:01

Gloria Santin


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


1 Answers

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); }
}
like image 75
JaredPar Avatar answered Nov 03 '22 00:11

JaredPar