Anonymous Methods
Added in .NET 2.0
These are superseded by Lambda Expressions
Anonymous methods allow you to define a delegate inline.
An anonymous method provides a way of passing a block of code as an argument instead of creating an explicit named method.
Use the delegate keyword to declare a delegate type first
A parameter is then passed to an anonymous method using the delegate keyword
no parameters
delegate void Delegate_Type();
Delegate_Type delegate_variable = delegate()
{ // do something }
instead of
delegate void Delegate_Type();
void Method_Name()
{ // do something }
Delegate_Type delegate_variable = Method_Name;
with a parameter
delegate int Delegate_Type(int x);
Delegate_Type delegate_variable = delegate(int paramater)
{ return paremeter * parameter; }
instead of
delegate int Delegate_Type(int x);
int Method_Name(int parameter)
{ return parameter * parameter; }
Delegate_Type delegate_variable = Method_Name;
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext