Articles → .NET → Call private method of the class using delegates in c#

Call private method of the class using delegates in c#






Technical knowledge





Create a class with private method




public class MyClass {
	private void Display() {
		Console.WriteLine("Private Display Method");
	}
}





Add delegate




public class MyClass {

	public MyClass() {
		DelegateProp = new MyDelegate(Display);
	}
	public delegate void MyDelegate();
	private void Display() {
		Console.WriteLine("Private Display Method");
	}

	public MyDelegate DelegateProp {
		get;
		set;
	}

}





Call delegate




MyClass cls = new MyClass();
cls.DelegateProp();



Output



Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Sunday, April 19, 2015

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250