Back to basics!!! Can we have private interfaces in C#? What is private interface, use of it?

Interface members are by default public, private and public both type of interface implementation, possible in VB.NET but not in C#. C# only supports public interfaces

According to ECMA- C#
All interface members implicitly have public access. It is a compile-time error for interface member
declarations to include any modifiers. In particular, interface members cannot be declared with the modifiers abstract, public, protected, internal, private, virtual, override, or static.

Also as per the MSDN  (VB.net)You can use a private member to implement an interface member. When a private member implements a member of an interface, that member becomes available by way of the interface even though it is not available directly on object variables for the class.

As per the discussion on Social.msdn (Vb.net)
Visual Basic.NET requires that you explicitly tell the compiler that you want to implement an interface member. It does not assume that because the name of a member matches the name of an interface member, it will automatically implement the member.

For C#, you have two options. You can either implement the interface by providing public methods matching the name (and signature) of all interface members, or you can choose to explicitly implement the interface, in which case the members implementing the interface are all assumed to be private.

But we can have work around in case we want to use the Private Interfaces
We need to declare Interfaces as Private inside the class

public class PrivateInterfaceClass
{
private interface InterfaceTest
{
int MyMethod { get; }
void printMe();
}


private class CallTheMethod : InterfaceTest
{
#region InterfaceTest Members
public int MyMethod
{
get;
set;
}
public void printMe()
{
Console.WriteLine(“From CallTheMethod which Implements InterfaceTest “);
}
#endregion
}
public static void Main()
{
InterfaceTest objTest = new CallTheMethod();
objTest.printMe();
}
}

// Output
From CallTheMethod which Implements InterfaceTest
Press any key to continue . . .

Private interface, intended for use by only within class e.g. the above code (private interface) simply hides from other code, even in same assembly. This allows you to expose the nested classes in some fashion while allowing the parent class to invoke methods on them that the outside world cannot.
To provide special interfaces and restrict access to them, we can declare the interfaces, inherit them privately and then write access functions that provide (restricted) access to the interface.

Ref :
http://en.csharp-online.net/Common_Type_System%E2%80%94Private_Interface_Inheritance
http://accu.org/index.php/journals/580
http://stackoverflow.com/questions/792908/what-is-a-private-interface

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s