An interface is a contract. It says "I can do these things." What's the point of someone handing you an instance of IInterface
wherein you can't use some of the methods in that contract because they've been marked not public?
This is the rationale for designing the language in this way. The specification for this is in §13.2 of the language specification:
All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers. In particular, interfaces members cannot be declared with the modifiers abstract
, public
, protected
, internal
, private,
virtual
, override
, or static
.
As for your code, that is an example of explicit interface implementation. It is most useful for when a class or struct implements two interfaces each with a member having the same signature. For example, both IEnumerable
and IEnumerable<T>
define a method GetEnumerator
accepting no parameters.
public interface IEnumerable {
IEnumerator GetEnumerator();
}
public interface IEnumerable<T> : IEnumerable {
IEnumerator<T> GetEnumerator();
}
Note that by the above definitions, any class that implements IEnumerable<T>
must also implement IEnumerable
. Keep in mind that the return type is not part of the signature and thus we have a conflict with IEnumerable.GetEnumerator
and IEnumerable<T>.GetEnumerator
. This is what explicit interface implementation is meant to solve:
class X<T> : IEnumerable<T> {
List<T> _list = new List<T>();
public IEnumerator<T> GetEnumerator() {
return _list.GetEnumerator();
}
IEnumerator GetEnumerator() {
return GetEnumerator(); // invokes IEnumerable<T>.GetEnumerator
}
}
Members that are explicit interface implementations are only visible through an instance of the interface. Thus:
X<int> x = new X<int>();
var e1 = x.GetEnumerator(); // invokes IEnumerable<int>.GetEnumerator
// IEnumerable.GetEnumerator is not visible
IEnumerable y = x;
var e2 = y.GetEnumerator(); // invokes IEnumerable.GetEnumerator
Thus, in your code
X ob = new Y();
ob.add(1, 2); // X.add is visible through interface
Y y = new Y();
y.add(1, 2); // compile-time error, X.add is not visible
Both methods don't require modifiers but when I don't use interface, like X.add() above, I need to make the implementation public. Why?
Okay, it's not clear exactly what you're asking here. Access modifiers are not allowed for explicit interface implementations. This is 13.4:
It is a compile-time error for an explicit interface member implementation to include access modifiers, and it is a compile-time error to include the modifiers abstract
, virtual
, override
, or static
.
If a interface implementation is not marked as being an explicit interface implementation, then it must have the access modifier public
. This is 13.4.4 (Interface mapping):
Interface mapping for a class or struct C
locates an implementation for each member of each interface specified in the base class list of C
. The implementation of a particular interface member I.M
, where I
is the interface in which the member M
is declared, is determined by examining each class or struct S
, starting with C
and repeating for each successive base class of C
, until a match is located
If S
contains a declaration of an explicit interface member implementation that matches I
and M
, then this member is the implementation of I.M
Otherwise, if S
contains a declaration of a non-static public member that matches M
, then this member is the implementation of I.M
.
A compile-time error occurs if implementations cannot be located for all members of all interfaces specified in the base class list of C
.
So, in short, the compiler first looks for an explicit interface implementation. If it can not find one then it looks for a non-static, public member with the same signature as the method M
being implemented. If it can not find one a compile-time error occurs. So the rules are this. To implement an interface member I.M
:
If you implement I.M
explicitly then the syntax is
return-type I.M(parameter-list)
Otherwise, the syntax is
public return-type M(parameter-list)
Thus, with
interface IAdd {
int Add(int x, int y)
}
We can implement explicitly:
class Explicit : IAdd {
int IAdd.Add(int x, int y) { return x + y; }
}
or not:
class NotExplicit : IAdd {
public int Add(int x, int y) { return x + y; }
}
The difference is then that Explicit.Add
is not visible unless instances of Explicit
are typed as IAdd
:
IAdd explicitInterface = new Explicit();
explicitInterface.Add(2, 2);
Explicit explicit = new Explicit();
explicit.Add(2, 2); // compile-time error
whereas
IAdd notExplicitInterface = new NotExplicit();
notExplicitInterface.Add(2, 2);
NotExplicit notExplicit = new NotExplicit();
notExplicit.Add(2, 2); // okay, NOT a compile-time error as above
Does that help?