create a console application in c#.net and copy this
namespace ConsoleApplication1
{
class Program
{
//Declaring a delegate
delegate void Display(String text);
static void Main(string[] args)
{
//Anonymous method
Display d = delegate(String s)
{
Console.WriteLine(s + "Anonymous method");
};
//Calling anonymous method
d("I am calling ");
d = new Display(Program.Foo);
//Calling named method
d("I am calling ");
Console.ReadLine();
}
/// Named method
public static void Foo(String text)
{
Console.WriteLine(text + "Named Method.");
}
}
}
You cannot use a "ref" or "out" variable inside an anonymous method.
If you are using a variable that is declared outside the anonymous method is called "outer" or "captured" variables of the anonymous method.
eg:-
string outside = "I am captured";
//Anonymous method
Display d = delegate(String s)
{
Console.WriteLine(s + "Anonymous method " + outside);
};
No comments:
Post a Comment