Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have to name interface method parameters?

Tags:

In C# we have to name the parameters of a method of an interface.

I understand that even if we didn't have to, doing so would help a reader understand the meaning, however in some cases it's not really needed:

interface IRenderable {     void Render(GameTime); } 

I would say the above is as readable and meaningful as the below:

interface IRenderable {     void Render(GameTime gameTime); } 

Is there some technical reason why names for parameters of methods on an interface are required?


It's worth noting that the implementation of the interface method can use different names to those in the interface's method.

like image 494
George Duckett Avatar asked Dec 23 '11 09:12

George Duckett


People also ask

Should interface methods have parameters?

Implementing an InterfaceThe methods must have the exact same signature (name + parameters) as declared in the interface. The class does not need to implement (declare) the variables of an interface.

CAN interface have different parameters?

Yes, you can have overloaded methods (methods with the same name different parameters) in an interface.

What is parameter interface?

The parameter interface of a procedure consists of formal parameters and specifies the exceptions possible in the procedure. Formal Parameters. Formal parameters are input parameters, output parameters, input/output parameters, or return values.

Can we have final methods in interface?

No ;Interface only have abstract methods. methods. interface method can not be final .


2 Answers

One possible reason could be the use of optional parameters.

If we were using an interface, it would be impossible to specify named parameter values. An example:

interface ITest {     void Output(string message, int times = 1, int lineBreaks = 1); }  class Test : ITest {      public void Output(string message, int numTimes, int numLineBreaks)     {         for (int i = 0; i < numTimes; ++i)         {             Console.Write(message);             for (int lb = 0; lb < numLineBreaks; ++lb )                 Console.WriteLine();         }      } }  class Program {     static void Main(string[] args)     {         ITest testInterface = new Test();         testInterface.Output("ABC", lineBreaks : 3);     } } 

In this implementation, when using the interface, there are default parameters on times and lineBreaks, so if accessing through the interface, it is possible to use defaults, without the named parameters, we would be unable to skip the times parameter and specify just the lineBreaks parameter.

Just an FYI, depending upon whether you are accessing the Output method through the interface or through the class determines whether default parameters are available, and what their value is.

like image 192
Lukazoid Avatar answered Jan 27 '23 13:01

Lukazoid


I don't see any reason that would make this a technical requirement. But I can think of one particularly good reason:

As you mention, the parameter names are not needed when implementing the interface, and can be easily overridden.
However, when using the interface, imagine the difficulty if no parameters had meaningful names! No intellisense, no hints, nothing but a type? Yuck.
This has got to be the biggest reason that a name is always required.

like image 28
Scott Rippey Avatar answered Jan 27 '23 13:01

Scott Rippey