C#学习笔记:泛型委托Action<T>和Fun<TResult>
转自:http://www.cnblogs.com/Joetao/articles/2094271.html
本节学习了泛型委托Action<T>和Fun<TResult>两类特殊的委托,这两个特殊的委托是Dot FrameWrok自带的。结合lambda表达式,可以在写程序时,简洁代码和提高编码效率。
(一)Action<T>和Fun<TResult>两个委托的不同点:
Action<T>只能委托必须是无返回值的方法
Fun<TResult>只是委托必须有返回值的方法
(二)代码演练
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//方法一:显式声明了一个委托,并将对 实例方法的引用分配给其委托实例。
public delegatevoid ShowName();
public delegatevoid ShowNameWithParameter(string name);
public delegatevoid ShowAge(int age);
public delegatevoid ShowNameAndAge(string name, int age);
public delegateint ReturnName(string name);
namespace ActionDemo
{
publicclass Person
{
privatestring instanceName;
publicstring InstanceName
{
get { return instanceName; }
set { instanceName = value; }
}
privateint instanceAge;
publicint InstanceAge
{
get { return instanceAge; }
set { instanceAge = value; }
}
public Person(string name,int age)
{
this.instanceName = name;
this.instanceAge = age;
}
publicvoid DisplayName()
{
Console.WriteLine("Name:{0}",this.instanceName);
}
publicvoid DisplayName(string name)
{
Console.WriteLine("Name:{0}",name);
}
publicvoid DisplayAge(int age)
{
Console.WriteLine("Age:{0}",age);
}
publicvoid DisplayNameAndAge(string name, int age)
{
Console.WriteLine(string.Format("Name:{0} And Age:{1} ",name,age));
}
publicint GetAgeByName(string name)
{
if(name==instanceName)
{
return instanceAge;
}
else
{
return-1;
}
}
}
class Program
{
privatestaticvoid Print(string s)
{
Console.WriteLine(s);
}
staticvoid Main(string[] args)
{
#region Action<T>相关
Person person =new Person("joetao",21);
//非泛型委托
//ShowName showName = new ShowName(name.DisplayName);
//另一种写法
ShowName showName = person.DisplayName;
showName();
ShowNameWithParameter showNameWithParameter = person.DisplayName;
showNameWithParameter(person.InstanceName);
ShowAge showAge = person.DisplayAge;
showAge(person.InstanceAge);
ShowNameAndAge showNameAndAge = person.DisplayNameAndAge;
showNameAndAge(person.InstanceName, person.InstanceAge);
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//方法一:显式声明了一个委托,并将对 实例方法的引用分配给其委托实例。
public delegatevoid ShowName();
public delegatevoid ShowNameWithParameter(string name);
public delegatevoid ShowAge(int age);
public delegatevoid ShowNameAndAge(string name, int age);
public delegateint ReturnName(string name);
namespace ActionDemo
{
publicclass Person
{
privatestring instanceName;
publicstring InstanceName
{
get { return instanceName; }
set { instanceName = value; }
}
privateint instanceAge;
publicint InstanceAge
{
get { return instanceAge; }
set { instanceAge = value; }
}
public Person(string name,int age)
{
this.instanceName = name;
this.instanceAge = age;
}
publicvoid DisplayName()
{
Console.WriteLine("Name:{0}",this.instanceName);
}
publicvoid DisplayName(string name)
{
Console.WriteLine("Name:{0}",name);
}
publicvoid DisplayAge(int age)
{
Console.WriteLine("Age:{0}",age);
}
publicvoid DisplayNameAndAge(string name, int age)
{
Console.WriteLine(string.Format("Name:{0} And Age:{1} ",name,age));
}
publicint GetAgeByName(string name)
{
if(name==instanceName)
{
return instanceAge;
}
else
{
return-1;
}
}
}
class Program
{
privatestaticvoid Print(string s)
{
Console.WriteLine(s);
}
staticvoid Main(string[] args)
{
#region Action<T>相关
Person person =new Person("joetao",21);
//非泛型委托
//ShowName showName = new ShowName(name.DisplayName);
//另一种写法
ShowName showName = person.DisplayName;
showName();
ShowNameWithParameter showNameWithParameter = person.DisplayName;
showNameWithParameter(person.InstanceName);
ShowAge showAge = person.DisplayAge;
showAge(person.InstanceAge);
ShowNameAndAge showNameAndAge = person.DisplayNameAndAge;
showNameAndAge(person.InstanceName, person.InstanceAge);
string> actionShowName1 = person.DisplayName;
actionShowName1(person.InstanceName);
Action<int> actionShowAge = person.DisplayAge;
actionShowAge(person.InstanceAge);
Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge;
actionShowNameAndAge(person.InstanceName, person.InstanceAge);
actionShowName1(person.InstanceName);
Action<int> actionShowAge = person.DisplayAge;
actionShowAge(person.InstanceAge);
Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge;
actionShowNameAndAge(person.InstanceName, person.InstanceAge);
Action<string> actionShowName1 = person.DisplayName;
actionShowName1(person.InstanceName);
Action<int> actionShowAge = person.DisplayAge;
actionShowAge(person.InstanceAge);
Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge;
actionShowNameAndAge(person.InstanceName, person.InstanceAge);string> actionShowName1 = person.DisplayName;
actionShowName1(person.InstanceName);
Action<int> actionShowAge = person.DisplayAge;
actionShowAge(person.InstanceAge);
Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge;
actionShowNameAndAge(person.InstanceName, person.InstanceAge);