Sometimes
you may want to call the private method declared in one class in another class
in c#. Technically it is not possible to access private members of a class
outside the class in c#. Either you will need to modify the access specifier of
the class member from private to something else. However if say you are not allowed to change
the access specifier of private method but still want to provide a mechanism to
enable calling of private method in another class? How can w achieve this in
c#? It is very well possible in C# with the help of Delegates.
Delegates
are nothing but pointer to method. If you want to get value of private variable,
we can declare a public property similarly if you want to call private method outside
the declaring class we can use public property returning delegate (or pointer
to the method).
Following
construct depicts how delegates can be used to access private method outside a
class. Let’s say I have a class A having private method as below –