Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...