The concept of anonymous method was introduced in C# 2.0.
An anonymous method is inline unnamed method in the code.
It is created using the
delegate
keyword and doesn’t required name and return type.
Hence we can say, an anonymous method has only body without
name, optional parameters and return type. An anonymous method behaves like a
regular method and allows us to write inline code in place of explicitly named
methods.
Anonymous
methods in C# can be defined using the delegate keyword and can be assigned to
a variable of delegate type.
Anonymous
methods provide a technique to pass a code block as a delegate parameter.
Anonymous
methods are the methods without a name, just the body.
You
need not specify the return type in an anonymous method; it is inferred from
the return statement inside the method body.
publicdelegate
void
Print(
intvalue
);
staticvoid
Main(
string[] args)
{
p =
delegate(
intval) {
Console
.WriteLine(
"Inside Anonymous method. Value: {0}", val);
};
p(100);
}
#2. namespace AnonymousMethods
{
public class MyClass
{
public delegate void MyDelegate(string
message);
public event MyDelegate
MyEvent;
public void RaiseMyEvent(string
msg)
{
if
(MyEvent != null)
MyEvent(msg);
}
}
class Caller
{
static void Main(string[]
args)
{
MyClass
myClass1 = new MyClass();
//Register
event handler as anonymous method.
myClass1.MyEvent += delegate
{
Console.WriteLine("we don't make use of your message in the first
handler");
};
//Register
event handler as anonymous method.
//here we
make use of the incoming arguments.
myClass1.MyEvent += delegate(string
message)
{
Console.WriteLine("your message is: {0}", message);
};
//the final bracket of the anonymous method must be terminated by a semicolon.
//the final bracket of the anonymous method must be terminated by a semicolon.
Console.WriteLine("Enter Your Message");
string
msg = Console.ReadLine();
//here is
we will raise the event.
myClass1.RaiseMyEvent(msg);
Console.ReadLine();
}
}
}
}
Anonymous methods can access variables defined in an
outer function.
public delegate void
Print(int value);
static void Main(string[]
args)
{
int i = 10;
Print prnt = delegate(int val) {
val += i;
Console.WriteLine("Anonymous method:
{0}", val);
};
prnt(100);
}
Anonymous methods can
also be passed to a method that accepts the delegate as a parameter.
In the following
example, PrintHelperMethod() takes the first parameters of the Print delegate:
public delegate void Print(int
value);
class Program
{
public static void PrintHelperMethod(Print
printDel,int val)
{
val += 10;
printDel(val);
}
static void Main(string[] args)
{
PrintHelperMethod(delegate(int val) { Console.WriteLine("Anonymous
method: {0}", val); }, 100);
}
}
Anonymous methods can be used as event handlers:
saveButton.Click +=
delegate(Object o, EventArgs e) {
System.Windows.Forms.MessageBox.Show("Save
Successfully!");
};
protected void Page_Load(object sender, EventArgs e)
{
// Click Event handler using Regular method
btnCancel.Click += new EventHandler(ClickEvent);
// Click Event handler using Anonymous method
btnSubmit.Click += delegate {
lblmsg.Text="Submit Button clicked using Anonymous method";
};
}
protected void ClickEvent(object sender, EventArgs e)
{
lblmsg.Text="Cancel Button clicked using Regular method";
}
Key points about anonymous method
1.
A
variable, declared outside the anonymous method can be accessed inside the
anonymous method.
2.
A
variable, declared inside the anonymous method can’t be accessed outside the
anonymous method.
3.
We
use anonymous method in event handling.
4.
An
anonymous method, declared without parenthesis can be assigned to a delegate
with any signature.
5.
Unsafe
code can’t be accessed within an anonymous method.
6.
An
anonymous method can’t access the ref or out parameters of an outer scope.
Anonymous method limitations:
- It cannot contain jump statement like goto, break or continue.
- It cannot access ref or out parameter of an outer method.
- It cannot have or access unsafe code.
- It cannot be used on the left side of the is operator.
0 comments:
Post a Comment