ASP.NET学习之匿名方法
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AnonymousMethod
{
///<summary>
/// apply a delegate!
///</summary>
///<param name="s"></param>
delegatevoid WriteString(string s);
class Program
{
staticvoid Main(string[] args)
{
//define an instance of delegate, which is anonymous
WriteString print =delegate(string j)
{
Console.WriteLine(j +" of Anonymous Method!");
};
//call the anonymouse methode
print("The delegate using the anonymous method is called.");
//rederict the method to a named methode
print =new WriteString(Program.NamedMethode);
//call the name methode
print("The delegate using the Named Methode is called.");
int? i =null;
if (!i.HasValue)
{
Console.WriteLine("oh!i have nothing");
}
Console.Read();
}
///<summary>
/// define a name methode
///</summary>
///<param name="k"></param>
staticvoid NamedMethode(string k)
{
System.Console.WriteLine(k);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AnonymousMethod
{
///<summary>
/// apply a delegate!
///</summary>
///<param name="s"></param>
delegatevoid WriteString(string s);
class Program
{
staticvoid Main(string[] args)
{
//define an instance of delegate, which is anonymous
WriteString print =delegate(string j)
{
Console.WriteLine(j +" of Anonymous Method!");
};
//call the anonymouse methode
print("The delegate using the anonymous method is called.");
//rederict the method to a named methode
print =new WriteString(Program.NamedMethode);
//call the name methode
print("The delegate using the Named Methode is called.");
int? i =null;
if (!i.HasValue)
{
Console.WriteLine("oh!i have nothing");
}
Console.Read();
}
///<summary>
/// define a name methode
///</summary>
///<param name="k"></param>
staticvoid NamedMethode(string k)
{
System.Console.WriteLine(k);
}
}
}