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

Not sure if this is a stupid 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 my implementation, I named my second parameter โ€œmaxRecordsโ€. However, in the interface it is defined as an โ€œaccountโ€. The compiler still considers the implemented interface, which is good, but can lead to some ambiguity. It is clear that I have to rename one of the parameters in accordance with the other.

I played a little before renaming and noticed something interesting. I am not allowed to declare my interface as:

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

Is it just a compiler that is too protected from C # symantics? What purpose do parameter names use in an interface method, except to make the code more readable? It seems to me that it causes ambiguity if the parameter names are not enforced during implementation.

+11
c #
Sep 04
source share
5 answers

Parameter names are required in the interface declaration for clarity of implementation and for reference. If someone used your interface, the method parameter names are intended for self-documentation, so the user of the interface understands what to pass to the method (for example, when viewing the method description through IntelliSense)

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

+19
Sep 04 '12 at 23:45
source share

Story. This goes back to the earliest days of .NET when COM ruled the world. The ability to interact with COM was very important then, no one ever drops everything to adopt a completely new programming style.

What made COM interoperability strongly supported in .NET in general. In addition to having named arguments for interface methods, they require type libraries.

An interesting corner case forever is the C ++ / CLI language. He 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); }; 

An exporter of the type library generates this declaration:

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

A very similar result if you implement an interface in a C # class, as IntelliSense autogenerates:

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

This does not please anyone.

+8
Sep 05
source share

I would suggest that this is due to the function of the named parameters in C #. Those. you need to specify the parameters by name, and not just in the default order:

 IActivityDao dao; dao.GetAllSinceSequence(count: 1, sequence: 2); 

Of course, the parameter names would be different if the object was selected as your instance.

 var concreteDao = (ActivityDao) dao; concreteDao.GetAllSinceSequence(maxRecords: 1, sequence: 2); 
+2
Sep 04 '12 at 23:45
source share

Let me ask you this, is there anywhere else in the .net infrastructure that allows you to define a method signature without parameter names?

In the end, everything is possible, but most things are for some reason, in which case I would assume that this is the limit of the structure and design of the compiler, and does it really matter?

You still define a contract for use, one would expect that they really will be there.

+2
Sep 04
source share

Many languages, such as C # and VB, support parameter names and arguments for methods . Without argument names in the interface, using names and optional arguments would be impossible. Naming arguments also helps the reader understand the intent and function of the interface.

+1
Sep 04 2018-12-12T00:
source share



All Articles