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)的更多相关文章

  1. Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)

    Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...

  2. C#的委托 Action<>和Func<>

    其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: [csharp]  public delegate vo ...

  3. (C#) Action, Func, Predicate 等泛型委托

    (转载网络文章) (1). delegate delegate我们常用到的一种声明   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.   例:public del ...

  4. 委托delegate,Action,Func,Predicate

    C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...

  5. C# 委托应用总结(委托,Delegate,Action,Func,predicate)

    C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...

  6. c# Action,Func,Predicate委托

    System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...

  7. VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试

    [提示] 1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的 Action与Func委托.2.如果您对单 ...

  8. Delegate,Action,Func,Predicate的使用与区别

    C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...

  9. Lamda Action Func Thread 实例

    lamda表达式 格式:( 形参列表 ) => { 函数体 } 作用:简化匿名方法的书写,可用在任何可使用匿名方法和强类型代理的地方: Action是无返回值的泛型委托. Action 表示无参 ...

随机推荐

  1. Android USB Connections Explained: MTP, PTP, and USB Mass Storage

    Android USB Connections Explained: MTP, PTP, and USB Mass Storage Older Android devices support USB ...

  2. Scheme vs Schema

    在计算机数据描述领域,Scheme由于原意为“主题”,“方案”,“构想”等,因此一般指比较明确的(具体的)“方案”.“体系”,例如一个术语词表.分类表等,而Schema通常翻译成“模式”,比较强调形式 ...

  3. GSM cell phone calls use outdated encryption that can now be cracked with rainbow tables on a PC

    Decrypting GSM phone calls Motivation. GSM telephony is the world’s most popular communication techn ...

  4. BZOJ 1568 Blue Mary开公司

    李超线段树. GTMD调了一下午... #include<iostream> #include<cstdio> #include<cstring> #include ...

  5. BZOJ 3439 Kpm的MC密码

    倒着建trie,然后主席树来求子树第k大. #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  6. 爬虫再探实战(三)———爬取动态加载页面——selenium

    自学python爬虫也快半年了,在目前看来,我面临着三个待解决的爬虫技术方面的问题:动态加载,多线程并发抓取,模拟登陆.目前正在不断学习相关知识.下面简单写一下用selenium处理动态加载页面相关的 ...

  7. 为什么要urlencode

    为什么要urlencode  1.为了正常获取值  字符 特殊字符的含义 URL编码 & 分隔不同的变量值对 %26 = 用来连接键和值 %3D ? 表示查询字符串的开始 %3F # 用来标志 ...

  8. HDU 3357

    http://acm.hdu.edu.cn/showproblem.php?pid=3357 给出公司间的控股关系,问有多少组不合法数据,自己控股自己不合法,a控股b,b控股c,则a控股c 其实就是找 ...

  9. 【转】C 宏

    http://www.cs.yale.edu/homes/aspnes/pinewiki/C%282f%29Macros.html See KernighanRitchie Appendix A12. ...

  10. M1: 创建UWP空项目

    本小节介绍如何在Visual Studio中创建一个UWP项目,Visual Studio中提供了快速创建各种项目的类型模板.在Visual Studio 2015中,同样提供了UWP项目模板. 学完 ...