Articles → CSHARP → Anonymous Methods In C#

Anonymous Methods In C#






Example


public class AnonymousDelegate: Page {
	public AnonymousDelegate() {
		btnSubmit.Click += new EventHandler(btnSubmit_Click);
	}

	private void btnSubmit_Click(object sender, EventArgs e) {
		// ... Code         
	}
}




public class AnonymousDelegate: Page {
	protected void Page_Init(object sender2, EventArgs e2) {
		//       Anonymous method
		btnSubmit.Click += delegate(object sender, EventArgs e) {...Code
		};
	}
}




public class AnonymousDelegate: Page {
	protected void Page_Init(object sender2, EventArgs e2) {
		string str = "Test";
		//       Anonymous method
		btnSubmit.Click += delegate(object sender, EventArgs e) {
			Response.Write(str);
		};
	}
}





Attaching Event To The Delegate


public class AnonymousDelegate: Page {
	protected void Page_Init(object sender2, EventArgs e2) {
		string str = "Test";
		//       Anonymous method        
		EventHandler clickHandler = delegate(object sender, EventArgs e) {
			Response.Write(str);
		};
	}
	button1.Click += clickHandler;
	button2.Click += clickHandler;
}



Posted By  -  Karan Gupta
 
Posted On  -  Thursday, January 21, 2010

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250