4

Possible Duplicate:
Non Public Members for C# Interfaces

Suppose I have

internal interface IInterface
{
    int MyProperty { get; set; }
}

public class MyClass : IInterface
{
    internal int MyProperty
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}

I get this:

does not implement interface member; cannot implement an interface member because it is not public;

I know what's the fix, but I am wondering why doesn't C# let interface members to be private.

Many times over I wanted my classes to follow a pattern, but needn't expose the members to public, say when I am writing a library. And best of all, the Interface itself is not public :X

Note: I am not asking how to implement private interface members, but I am knowing the design logic that went behind this decision. I couldn't find a suitable duplicate.

Update: More than the original thread, this code sample from the answer in another thread explains it better than most description answers. Or even better, from @JonSkeet this

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • I was posting an answer, but your question has already been closed :( You need an _explicit_ interface implementation. To use this feature, prefix the member you are implementing with the interface name. See this post for more details: [How to Implement an Interface Without Making Members Public Using Explicit Interface Implementation](http://blogs.msdn.com/b/johnwpowell/archive/2008/06/13/how-to-implement-an-interface-without-making-members-public-using-explicit-interface-implementation.aspx) – Paolo Moretti Sep 24 '12 at 10:53
  • @PaoloMoretti I am asking for the design decision, not knowing how to implement. – nawfal Sep 24 '12 at 11:17
  • You are essentially asking for the minutes of the meeting of the C# language designers when the section 13.4.4 (interface mapping) was written. If your question can only be answered definitively by someone who was in the room that day, then you're going to have to find someone who was in that room. – Paolo Moretti Sep 24 '12 at 12:57
  • @PaoloMoretti need not be. I was knowing if there is any logical complexity/ambiguity/foolishness in thinking of non public interface members which I couldn't think of. And yes there is, [here is one](http://stackoverflow.com/a/7238707/661933) which most people fail to explain – nawfal Sep 24 '12 at 19:17

2 Answers2

1

An interface is used to define a contract, by making the fields/methods private there is really no point in using an interface then. How does the client know how to use the contract? Unless you really need an abstract class.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • Does an abstract class force compiler error checking if child classes don't implement all the members of the parent abstract class? – nawfal Sep 24 '12 at 11:51
  • Haplessly had to accept this answer. See my update for better explanation – nawfal Sep 24 '12 at 12:07
1

The point of interfaces is that they provide a contract that other objects can use to communicate with your object. There would be not point of making the members private because it would not be useful anymore

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70