委托的示例
来源:互联网 发布:服务器同步备份软件 编辑:程序博客网 时间:2024/05/21 19:30
Action 是一个委托的方法,他是没有返回值类型的,他是只适用于viod的函数。
Func 也是委托方法,他有有返回值类型的,
注意: 比如Func<int,int,int> 这里面你的三个int参数,前两个代表参数类型,最后一个永远是返回值类型。
观察者模式与委托,事件
加上event只能类里面触发,不能再类的外部触发。
不加event都能触发.
示例:
class MainClass
{
static int Text1 (int arg1, int arg2)
{
return arg1 + arg2;
}
static int Text2 (string str)
{
Console.WriteLine (str);
return 100;
}
public static void Main (string[] args)
{
Func<int,int,int> plus = Text1;
int a = plus(20,23);
Console.WriteLine (a);
Console.ReadKey ();
Func<string,int> plus = Text2;
int a = plus ("我输入了100");
Console.WriteLine (a);
}
}
多播委托:
他只会返回最后使用的返回值
示例:
class MainClass
{
static void Text1 ()
{
Console.WriteLine ("Text1");
}
static void Text2 ()
{
Console.WriteLine ("Text2");
}
public static void Main (string[] args)
{
Action a = Text1;
a += Text2;
a-=Text;
//如果该委托没有绑定任何方法,那就会出现异常。
if (a != null) {
a ();
}
Console.ReadKey ();
}
}
}
Lambda表达式:
class MainClass
{
public static void Main (string[] args)
{
Lambda表达式代替匿名方法,所以一个lAMbda表达式也是定义了一个方法
Func<int,int,int> plus = delegate(int arg1, int arg2) {
};
LAMBDA表达式是不需要声明类型的;
Func<int,int,int>plus=(args1,arg2)=>
{
return args1+arg2;
};
int s = plus (20,28);
Console.WriteLine (s);
一个参数的表达方式
Func<int,int>puls=x=>
{
return args1-12;
};
等价与
Func<int,int>puls=x=>x+2;
int s = puls (32);
Console.WriteLine (s);
Console.ReadKey ();
}
}
}
Func 也是委托方法,他有有返回值类型的,
注意: 比如Func<int,int,int> 这里面你的三个int参数,前两个代表参数类型,最后一个永远是返回值类型。
观察者模式与委托,事件
加上event只能类里面触发,不能再类的外部触发。
不加event都能触发.
示例:
class MainClass
{
static int Text1 (int arg1, int arg2)
{
return arg1 + arg2;
}
static int Text2 (string str)
{
Console.WriteLine (str);
return 100;
}
public static void Main (string[] args)
{
Func<int,int,int> plus = Text1;
int a = plus(20,23);
Console.WriteLine (a);
Console.ReadKey ();
Func<string,int> plus = Text2;
int a = plus ("我输入了100");
Console.WriteLine (a);
}
}
多播委托:
他只会返回最后使用的返回值
示例:
class MainClass
{
static void Text1 ()
{
Console.WriteLine ("Text1");
}
static void Text2 ()
{
Console.WriteLine ("Text2");
}
public static void Main (string[] args)
{
Action a = Text1;
a += Text2;
a-=Text;
//如果该委托没有绑定任何方法,那就会出现异常。
if (a != null) {
a ();
}
Console.ReadKey ();
}
}
}
Lambda表达式:
class MainClass
{
public static void Main (string[] args)
{
Lambda表达式代替匿名方法,所以一个lAMbda表达式也是定义了一个方法
Func<int,int,int> plus = delegate(int arg1, int arg2) {
};
LAMBDA表达式是不需要声明类型的;
Func<int,int,int>plus=(args1,arg2)=>
{
return args1+arg2;
};
int s = plus (20,28);
Console.WriteLine (s);
一个参数的表达方式
Func<int,int>puls=x=>
{
return args1-12;
};
等价与
Func<int,int>puls=x=>x+2;
int s = puls (32);
Console.WriteLine (s);
Console.ReadKey ();
}
}
}
0 0
- 委托的示例
- 简单的委托示例
- 委托的示例
- 最精简的委托示例
- c#的委托代码示例
- c#的委托代码示例
- 委托的一個示例
- 委托的一段示例代码
- 委托示例
- 委托示例
- 委托示例
- 委托示例(利用委托对不同类型的对象数组排序)
- 利用委托的一个实例-----BubbleSorter示例
- [VB.Net]委托的一个简单示例
- c#委托的异步调用 简单示例
- C#委托最简单的示例!
- C#委托示例
- 委托示例代码(1)
- MacOS之safari无法显示图片
- ORACLE DATABASE 11G RAC 知识图文详细教程之集群概念介绍(一)
- 第六章 异常 try-catch-finally 6.2.3
- jhipster整合mybatis
- properties配置文件
- 委托的示例
- c# 人脸框截图处理
- 3.5 JS设置节点属性
- 做项目中遇到一个小问题 (要让容器与浏览器宽度,高度相等)
- SRWebSocket源码浅析
- 自动登录过BSO的脚本
- js 事件绑定on()的用法,以及对于其他绑定事件的比较
- The Balance (母函数)
- Jboss下使用spring配置实现JTA全局事务管理(下)