3

Methods in the interface and in the class implementing the interface are public by default. They do not need the accessor mentioned.

Why do properties, specifically in the class implementing the interface, need to have the public access modifier mentioned? Other modifiers aren't allowed anyway.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Shivku
  • 156
  • 3
  • 10
  • Who told you that methods are public by default, they need access specifier in the class implementing the interface. – Ipsit Gaur Mar 13 '18 at 05:35
  • 1
    That's not accurate. Anything in an interface is public, even if the interface itself is internal, so you can't set any access modifier to any member of an interface. In a class implementing an interface, any method that explicitly implement the interface is public and you can't set any access modifier to it as well, but for any member that implicitly implements the interface you must specify the public access modifier. Any other modifier will generate a compilation error. – Zohar Peled Mar 13 '18 at 05:40
  • Good question! I have my assumptions as you can read in my answer, but I wonder what would @EricLippert have to say about it. – Zohar Peled Mar 13 '18 at 08:59
  • @IpsitGaur: Apologies for that error. Yes methods need an access specifier. It is only in the case of explicit interface implementation that they do not need it mentioned. – Shivku Mar 14 '18 at 15:29

4 Answers4

5

That's not accurate.
Anything in an interface is public, even if the interface itself is internal, so you can't set any access modifier to any member of an interface.

In a class implementing an interface, any member (property, method, event or indexer) that explicitly implement the interface is public and you can't set any access modifier to it as well.
But any member that implicitly implements the interface you must specify an access modifier.

Since everything in an interface is public, and you can't overload methods based on their access modifiers, Any other modifier will generate a compilation error.

Why does the compiler forces you to declare implicit interface implementation members as public?

Well, I'm not sure about the reason, but I assume that's because the default access modifier for class members is private, and allowing programmers to implicitly implement interfaces without specifying the public access modifier would mean that the c# compiler team must put in some extra work to make that happen, and (I think) more importantly, has a potential to confuse any developer looking at the code, Given that the implementation is implicit and without knowing the interface you can't know if a method in the class is an implementation of the interface or just a regular method.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
1

Refer the documentation: Access Modifiers (C# Programming Guide)

Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.

Enumeration members are always public, and no access modifiers can be applied.

Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.

Refer these:
Why do interface members have no access modifier?
Access modifiers on interface members in C#
I'm confused about default access modifier of C# interface members

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

In C#, Interface is made for the giving functionality as third party so it is always public member function.

while class has access modifier so it has private ,internal, protected,public.

also for giving access modifier to the property is help to make read-only using setter.

Mukesh Methaniya
  • 752
  • 1
  • 5
  • 13
0

It will violate the principle of Interface. This way you can hide the method declared in interface and its implementation from Class object which has implemented the interface. Which is not permitted.

Pratik
  • 1
  • 2