Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non Public Members for C# Interfaces [closed]

Tags:

c#

.net

interface

In C#, when you implement an interface, all members are implicitly public. Wouldn't it be better if we could specify the accessibility modifier (protected, internal, except private of course), or should we just use an abstract class instead?

like image 515
jfs Avatar asked Aug 20 '08 08:08

jfs


4 Answers

If an interface is internal, all its members will be internal to the assembly. If a nested interface is protected, only the subclasses of the outer class could access that interface.

Internal members for an interface outside of its declaring assembly would be pointless, as would protected members for an interface outside of its declaring outer class.

The point of an interface is to describe a contract between a implementing type and users of the interface. Outside callers aren't going to care and shouldn't have to care about implementation, which is what internal and protected members are for.

For protected members that are called by a base class, abstract classes are the way to go for specifying a contract between base classes and classes that inherit from them. But in this case, implementation details are usually very relevant, unless it's a degenerate pure abstract class (where all members are abstract) in which case protected members are useless. In that case, go with an interface and save the single base class for implementing types to choose.

like image 151
Mark Cidade Avatar answered Nov 12 '22 01:11

Mark Cidade


You can hide the implementation of an interface by explicitly stating the interface name before the method name:

public interface IInterface {
    public void Method();
}

public class A : IInterface {
    public void IInterface.Method() {
        // Do something
    }
}

public class Program {
    public static void Main() {
        A o = new A();
        o.Method(); // Will not compile
        ((IInterface)o).Method(); // Will compile
    }
}
like image 27
samjudson Avatar answered Nov 12 '22 01:11

samjudson


Would not make sense. An Interface is a contract with the public that you support those methods and properties. Stick with abstract classes.

like image 5
Ishmaeel Avatar answered Nov 11 '22 23:11

Ishmaeel


All the answers here more or less say that's how interfaces are meant to be, they are universal public specifications.

This being the most discussed thread, let me post two excellent answers I found on SO when this question surfaced my mind.

This answer gives an example of how it can be nonsensical to have non uniform access specifiers for interface members in derived classes. Code always better than technical descriptions.

To me the most damning thing about forced public interface members are that the interface itself can be internal to an assembly but the members it exposes have to be public. Jon Skeet explains here that's by design sadly.

That raises the question why weren't interfaces designed to have non-public definitions for members. That can make the contract flexible. This is pretty useful when writing assemblies where you dont want specific members of classes to be exposed to outside the assembly. I do not know why.

like image 5
nawfal Avatar answered Nov 11 '22 23:11

nawfal