0

Possible Duplicate:
Why Explicit Implementation of a Interface can not be public?

I read this Question. Straight from the question

interface IRepository<T>
{
    void AddString();
}

interface IStringRepo : IRepository<string>
{
    List<string> GetStrings();
}

public class BLL : IStringRepo
{
    public List<string> FilterStrings()
    {
        return new List<string>() { "Hello", "World" };
    }

    public List<string> IStringRepo.GetStrings()
    {
        throw new NotImplementedException();
    }

    public void IRepository<string>.AddString()
    {
        throw new NotImplementedException();
    }
}

Why does making an explicitly referenced member public is an Error?

Community
  • 1
  • 1
Sandeep
  • 7,156
  • 12
  • 45
  • 57
  • 2
    What do you expect the `public` modifier to do? You can(at least from normal C# code) only call this method through the interface, since it has no normal method name. – CodesInChaos Apr 15 '12 at 17:36
  • 1
    Because the language specification says so. – Jon Apr 15 '12 at 17:38
  • If you could make explicit interface implementation `public`, what would be the difference between that normal (implicit) interface implementation? – svick Apr 15 '12 at 17:46

3 Answers3

1

Why does making an explicitly referenced member public is an Error

This is by design, just imagine one of the most popular reasons which forces a developer to implement an interface explicitly: memeber name ambiguity, so you have an existing class which already exposes public member with some name, and you can't change an existing class API since it is used by other systems also you cannot change interface API (member names/signatures), and both class and interface defines a member with the same name, so providing public acces modifier for an interface member does not make sense since member with the same name already declared in a class.

MSDN, 13.4.1 Explicit interface member implementations

It is not possible to access an explicit interface member implementation through its fully qualified name in a method invocation, property access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.

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.

sll
  • 61,540
  • 22
  • 104
  • 156
0

Because they are not public so it's a conflict of a sort

You have a choice to implement either way - explicit or implicit.

If explicit it's often
because the implementer wants to force you to access it only through the interface and in a way to hide it from the public (simplified) - thus it's not public.

In short, it's one of the purposes of having the explicit implementation.

NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
-1

There is no any reason of making it public, cause by signing a contract with it you have no other choice then implement all its members like a public.

But why give a error?

Cause simply all interface members are public by definition.

Interfaces

Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers.

Tigran
  • 61,654
  • 8
  • 86
  • 123