Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...