• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#学习笔记:泛型委托ActionT和FunTResult

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C语言结构体中属性的偏移量计算发布时间:2022-07-14
下一篇:
我用C++的理由——关于C和C++的选择发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap