Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, is it possible to implement an interface member using a member with a different name, like you can do in VB.NET?

Ok, this is a question I'm asking, not as in demonstrating good coding practices (this actually could be considered a bad practice) but rather in regards to 'can' it be done at all.

That said, in VB.NET you implement an interface like this...

Sub SomeInterfaceMember()
    Implements ISomeInterface.SomeInterfaceMember
End Sub

while in C# you do it explicitly like this (which can only be called via the interface)...

void ISomeInterface.SomeInterfaceMember(){}

or more simply, implicitly like this (in which case you can call it directly, or via the interface)...

void SomeInterfaceMember(){} // <-- This name matches the interface member

However, regarding VB, you can also do this, using any name you want for the member...

Sub SomeRandomMemberName() // <-- This name doesn't match the interface member
    Implements ISomeInterface.SomeInterfaceMember
End Sub

In other words, the method that handles the implementation can have a completely different name than the interface's member name.

I'm just wondering if there's something similar to this in C#. (Yes, I know I can simply do an explicit interface, then access it via another 'wrapper' member with a different name that simply delegates to it, but in VB.NET, you do both with a single method.)

So can that be done?

Mark

like image 330
Mark A. Donohoe Avatar asked Apr 04 '11 01:04

Mark A. Donohoe


People also ask

What does |= mean in C?

The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

What is ?: operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.

What is -= in C?

This operator is a combination of '-' and '=' operators. This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. Example: (a -= b) can be written as (a = a - b) If initially value stored in a is 8.


1 Answers

No, this isn't possible in C#. (You can't "explicitly implement an interface" in VB.NET either.)

like image 149
user541686 Avatar answered Nov 11 '22 04:11

user541686