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

Back to basics!!! How to get total number of objects is created for a Class

We can get the actual number of objects created for class throughout lifetime,this can be achieved as follows
We need to declare the static integer to store the number/count of the object created.
And we just need to increment integer/counter by 1 every time constructor is getting called.

class Program
{
public static void Main()
{
Console.WriteLine(“Number of Objetcs :- ” + SampleClass.noOfObjects);
SampleClass s1 = new SampleClass();
Console.WriteLine(“Number of Objetcs :- ” + SampleClass.noOfObjects);
SampleClass s2 = new SampleClass();
Console.WriteLine(“Number of Objetcs :- ” + SampleClass.noOfObjects);
}

public class SampleClass
{
public static int noOfObjects = 0;
// Add counter in Constructor,
// Each time class object is call Counter will get incremented and
// we can get final value of the number by printing/getting the value of
// noOfObjetcs.

public SampleClass()
{
noOfObjects++;
}
}

}

Number of Objetcs :- 0
Number of Objetcs :- 1
Number of Objetcs :- 2
Press any key to continue . . .

Please share, if you know any other ways to achieve this.