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
497 views
in Technique[技术] by (71.8m points)

c# - Is there a reason you can not define the access modifier on a method or in an interface?

The responsibility of the visibility of a method is relegated to the class that implements the interface.

public interface IMyInterface
{
  bool GetMyInfo(string request);
}

In C# set access modifier public, private or protected before the method GetMyInfo() generates the following error: The modifier 'private' is not valid for this item.

Is there a reason you can not define the access modifier on a method or in an interface?

(Question already asked in french here)

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

The interface defines a contract between an object and clients that call its members. A private method cannot be accessed by any other objects so it doesn't make sense to add it to the interface. All members of an interface are considered public for this reason.


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

...