software master at the intersection of technology, science and art

home

download

anonymous methods


Anonymous methods are methods wthout a method declaration , i.e. no name == anonymous. Anonymous methods are essentially a way to pass a code block as a delegate parameter. For example -

// Create a handler for a click event
button1.Click += delegate(System.Object o, System.EventArgs e) { System.Windows.Forms.MessageBox.Show("Click!"); };

or

// Create a delegate instance
delegate void Del(int x);
// Instantiate the delegate using an anonymous method
Del d = delegate(int k) { /* ... */ };


The scope of the parameters of an anonymous method is the anonymous-method-block. This restricts the type of statements. For further discussion Click Here Javascript/JQuery has used the same structure for quite some time.