委托,C#本身的委托(Action Func)
1.Action
分为带泛型的和不带泛型的,带泛型可传入任何类型的参数。
格式如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input; namespace Demo1
{
class Program
{
static void Main(string[] args)
{
//泛型委托
//Action : //带一个参数的
Action<string> ac = DelegateTest;
ac("带一个参数"); //带两个参数
Action<int, int> action = DelegateTest;
action(, ); Console.ReadKey();
} static void DelegateTest(string s)
{
Console.WriteLine(s);
}
static void DelegateTest(int a ,int b)
{
Console.WriteLine(a+b);
}
}
} 不带泛型,格式如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input; namespace Demo1
{
class Program
{
static void Main(string[] args)
{
// 无参数无返回值的委托 Action action1 = DelegateTest;
action(); Console.ReadKey();
} static void DelegateTest()
{
Console.WriteLine("无参的委托");
}
}
}
2.Func :
有参数 有返回值的委托 (参数的最后一个为返回值)
Func<int, int, int> objCall = ((a, b) => { return a * b; });
Func<int, int, int> objCall1 = ((a, b) => { return a / b; });
Action<int, int> ob = ((a, b) => { Console.WriteLine(a * b); });
ob(, );
//----------------------------------------------------//
int result = objCall(, );
int result1 = objCall1(, );
System.Console.WriteLine("结果1为 {0},结果2为{1}", result, result1);
// Lambda 表达式
Func<int, bool> dele1 = n => n > ;
// Lambda 语句
Func<int, bool> dele2 = (int n) => { return n > ; };
Console.WriteLine(dele1());
Console.WriteLine(dele1());
Console.ReadKey();
委托,C#本身的委托(Action Func)的更多相关文章
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...
- C#的委托 Action<>和Func<>
其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: [csharp] public delegate vo ...
- (C#) Action, Func, Predicate 等泛型委托
(转载网络文章) (1). delegate delegate我们常用到的一种声明 Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型. 例:public del ...
- 委托delegate,Action,Func,Predicate
C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...
- C# 委托应用总结(委托,Delegate,Action,Func,predicate)
C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...
- c# Action,Func,Predicate委托
System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...
- VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
[提示] 1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的 Action与Func委托.2.如果您对单 ...
- Delegate,Action,Func,Predicate的使用与区别
C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...
- Lamda Action Func Thread 实例
lamda表达式 格式:( 形参列表 ) => { 函数体 } 作用:简化匿名方法的书写,可用在任何可使用匿名方法和强类型代理的地方: Action是无返回值的泛型委托. Action 表示无参 ...
随机推荐
- memcpy的用法及实现
memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中,返回dest所指内存地址的起始位置. #include <string.h&g ...
- 分析一个嵌入payload的恶意.lnk文件
原文:https://isc.sans.edu/diary/Analyzis+of+a+Malicious+.lnk+File+with+an+Embedded+Payload/20763 We re ...
- matlab的正则表达式讲解[转]
引言.啥是正则表达式?正则表达式是干啥的?我理解就和我们在word或者其他编辑软件里点的查找.替换的作用是差不多的,不过功能要强大的多,当然使用起来也稍微复杂一些.书上的定义差不多是这样的:正则表达式 ...
- C118 免按开机自动加载固件
最近无事,研究了按按钮开机的功能:功能的起初是参考了别人的系统是怎么做免开机加载固件的. 一.原理: 1.c118 原生loader部分代码是没有源代码的,它上电只需要按开机键然后系统就会起来. 2. ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- 通过一个Thinkphp完成多个项目
1.单独取压缩包中的Thinkphp文件夹 2.在单独的项目内创建一个引入文件 3.通过浏览器访问该index.php 会创建相应的目录
- python 安装操作 MySQL 数据库.
以ubuntu和mysql为例 检查自己的机器上面有没有安装数据库 xpower@xpower-CW65S:~$ sudo service mysql start [sudo] xpower 的密码: ...
- BZOJ 2200 道路与航线
好厉害呀这道题,有种豁然开朗的感觉.... 按拓扑顺序跑最短路. 然后注意细节,像WA的代码犯下的错是一笔带过没有丝毫考虑的...然而就是错了. 考试的时候一定要拍啊. #include<ios ...
- __set()与__get() 魔术方法
在面向对象编程的过程中,对于类当中的各个成员变量,都有不同的访问属性,比如公有的(public)属性,在类内部和类外部都可直接调用:而私有的(private)和受保护的(protected),在类外不 ...
- 如何解决火狐FF里Input标签刷新页面后 仍然保存之前输入的内容的方法。
直接在input 标签里 增加 autocomplete="off".火狐默认为 on.