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

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.


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.


I would imagine this is due to the named parameters feature in C#. Ie, you need to be able to specify parameters by name, 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 is cast as your instance.

var concreteDao = (ActivityDao) dao;
concreteDao.GetAllSinceSequence(maxRecords: 1, sequence: 2);

Tags:

C#