码迷,mamicode.com
首页 > Windows程序 > 详细

C#学习笔记:泛型委托Action<T>和Fun<TResult>

时间:2016-08-14 14:20:29      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

转自: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);

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);

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);
 

C#学习笔记:泛型委托Action<T>和Fun<TResult>

标签:

原文地址:http://www.cnblogs.com/BluceLee/p/5769998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!