Hi,
I have a public interface. i have class that implements the interface, how do i make the implementation of interface members internal or private
in the implementation class?
If i declare an interface internal, how to implement that in an public class?
cheers

1 answers
The short answer to the first question is that you can't.
All the members of an interface are implicitly public, even though you can't specify public (or anything else) when declaring them within the interface.
When it comes to implementing the interface, the class (or struct) has to declare the interface members as public otherwise the compiler will complain.
Now, if you use 'explicit interface implementation' (as discussed in one of your previous threads), then the implementing class isn't allowed to use any access modifier on the interface member which is preceded by the name of the interface followed by a dot.
Such a member is nevertheless public provided you access it though an interface reference. It can't be accessed via a reference to the implementing class.
Moving on to your second question, if the interface is declared as internal then it can only be implemented by classes or structs declared within the same assembly. Moreover, any code outside the assembly won't be able to access the members of those classes or structs which are public using an interface reference.
However, everything else I've said above still stands for an internal interface. The following code works fine:
using System;
internal interface IFace
{
void MyMethod();
void MyMethod2();
}
public class Test : IFace
{
static void Main()
{
Test t = new Test();
t.MyMethod();
IFace iface = t;
iface.MyMethod2();
}
public void MyMethod()
{
Console.WriteLine("Hello");
}
void IFace.MyMethod2()
{
Console.WriteLine("Goodbye");
}
}
answered 2 years ago by:
17279
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!