Anonymous methods - simple example
Delegates were used as following way in earlier version of c#.net.
using System;
using System.Collections.Generic;
using System.Text;
namespace consAnonymousmethod
{
class Program
{
// delegate declaration
public delegate void mydelegate(string name);
static void Main(string[] args)
{
mydelegate md;
// delegate referencing function
md = Test.Greeting;
md("Surya");
Console.ReadLine();
}
class Test
{
// following function is being referenced by delegate
public static void Greeting(string name)
{
Console.WriteLine("hi "+name);
}
}
}
Using Anonymous methods it will be more simple
namespace consAnonymousmethod
{
class Program
{
public delegate void mydelegate(string name);
static void Main(string[] args)
{
mydelegate md;
/anonymous method
md = delegate(string name)
{
System.Console.WriteLine("Hi " + name + " : A message from the anonymous method.");
Console.ReadLine();
};
md("Surya");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace consAnonymousmethod
{
class Program
{
// delegate declaration
public delegate void mydelegate(string name);
static void Main(string[] args)
{
mydelegate md;
// delegate referencing function
md = Test.Greeting;
md("Surya");
Console.ReadLine();
}
class Test
{
// following function is being referenced by delegate
public static void Greeting(string name)
{
Console.WriteLine("hi "+name);
}
}
}
Using Anonymous methods it will be more simple
namespace consAnonymousmethod
{
class Program
{
public delegate void mydelegate(string name);
static void Main(string[] args)
{
mydelegate md;
/anonymous method
md = delegate(string name)
{
System.Console.WriteLine("Hi " + name + " : A message from the anonymous method.");
Console.ReadLine();
};
md("Surya");
}
}
}
Comments