Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are parameter names necessary in an interface definition? I am allowed to choose new parameter names during implementation

Tags:

c#

Not sure if this is a silly question, but I just noticed this:

public interface IActivityDao : IDao<Activity> {     IList<Activity> GetAllSinceSequence(long sequence, int count); }  public class ActivityDao : AbstractNHibernateDao<Core.Domain.Activity>, IActivityDao {             public IList<Activity> GetAllSinceSequence(long sequence, int maxRecords)     {      } } 

Inside of my implementation I have called my second parameter 'maxRecords.' Yet, in the interface, it is defined as 'count.' The compiler still consider the interface implemented, which is good, but can lead to a bit of ambiguity. Clearly, I should rename one of the parameters to match the other.

I played around a bit before making the rename and noticed something interesting. I'm not allowed to declare my interface as:

public interface IActivityDao : IDao<Activity> {     IList<Activity> GetAllSinceSequence(long, int); } 

Is this just the compiler being overly protective against C# symantics? What purpose do the parameter names in an interface's method serve other than to make the code more readable? It seems to me that it invites ambiguity if the parameter names aren't forced upon implementations.

like image 365
Sean Anderson Avatar asked Sep 04 '12 23:09

Sean Anderson


People also ask

Do parameter names and argument names have to be the same?

The caller's arguments passed to the function's parameters do not have to have the same names.

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.

Is it necessary to implement all methods of an interface in C#?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.

Why do we need interfaces in C#?

By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes.


2 Answers

Parameter names are required in an interface declaration for clarity of implementation and for reference. If someone were using your interface, the names of the method parameters are meant to be self documenting so the consumer of the interface understands what to pass to the method (eg when viewing the method description via IntelliSense)

And yes, when you implement the interface you can name the parameters whatever you want.

like image 67
user1438940 Avatar answered Sep 26 '22 01:09

user1438940


History. This goes back to the very early days of .NET, back when COM ruled the world. Being able to interop with COM was very important back then, nobody ever throws away everything to adopt an entirely new style of programming.

Which made COM interop strongly supported in .NET in general. As well as the need to have named arguments for interface methods, type libraries require them.

The interesting corner case is forever the C++/CLI language. It adopted many C++ syntax rules, including the ability to omit parameter names in declarations. In other words, this is legal:

    public interface class IFoo     {         void bar(int, long, double);     }; 

The type library exporter generates this declaration:

    HRESULT bar(                     [in] long p1,                      [in] long p2,                      [in] double p3); 

Very similar outcome if you implement the interface in a C# class, as autogenerated by IntelliSense:

class FooImpl : cpptemp36.IFoo {     public void foo(int __p1, int __p2, double __p3) {         throw new NotImplementedException();     } } 

This makes nobody happy.

like image 40
Hans Passant Avatar answered Sep 23 '22 01:09

Hans Passant