Articles → CSHARP → Call Private Method Of The Class Using Delegates In C#

Call Private Method Of The Class Using Delegates In C#






Create A Class With A 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


Picture showing the output of calling the private method of the class using delegate in C#



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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250