委托,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 表示无参 ...
随机推荐
- ZOJ 3329 - One Person Game
题意:每次筛三个骰子面分别为k1,k2,k3,如果三个骰子的值分别为a,b,c则得分置0,否则得到分数加上三个骰子的值的和,如果得分大于等于n则结束游戏. 设E[i]表示当前得到i分时结束游戏的期望. ...
- 2016 - 1 - 22 HTTP(一)
一:通过URL找到服务器 1. URL的概念: 1.1 URL全称Uniform Resource Locatior 统一资源定位符 1.2 通过一个URL就可以找到互联网上的唯一的资源. 1.3 ...
- Chapter 5: Container
Chapter 5: Container A container is a module that processes the requests for a servlet and populates ...
- 数据结构---设计一个栈,push, pop, min 时间复杂度都是 O(1)
普通的栈,push, pop 操作的复杂度是 O(1), 但是如果要找出其中的最小值,则需要 O(N)的时间. 题目要求 min 复杂度也是 O(1), 做法便是 空间换时间,每一步栈的最小值都用一个 ...
- Palindrome Number ---- LeetCode 009
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- jQuery中.parent和.parents的区别
.parent(selector) 获得当前匹配元素集合中每个元素的父元素,由选择器筛选(可选). .parents(selector) 获得当前匹配元素集合中每个元素的祖先元素,由选择器筛选(可选) ...
- iOS数据持久化
在iOS中,实现数据持久化一般分为4大种: 1.属性列表 2.对象归档 3.SQLite 4.Core Data 一.属性列表 NSUserDefaults类的使用和NSKeyedArchiver有很 ...
- 通讯录CoreData数据库实现版
一,创建新项目,选择空模板,名字MultyViewAddressBookCoreDataVersion,选择下面的use Core Data创建项目 首先在datamodel中创建一个实体,创建属性
- HDU 1026
http://acm.hdu.edu.cn/showproblem.php?pid=1026 记录bfs路径,用一个数组记录next前驱的方向,然后递归的时候减回去即可 #include <io ...
- 《JS高程》引用类型学习笔记
2月圆满的结束了,结束之前是如凤凰般的涅槃.一边上班,一边搞科研的忙碌有点让人透不过气,心会不由得浮躁起来.但是,无论什么事情,只要充满耐心.专心去做,总会朝好的方向发展,心态真的很重要.Anyway ...