委托调用方法的4种方式。

using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
delegate void DelFunc(string a);
//delegate void FUNC<int ,int,string>( );
class Program
{ public static void Fun1(string str)
{
List<int> list = new List<int>();
Dictionary<int, object> dic = new Dictionary<int, object>();
Console.WriteLine(str+"new");
}
public static void Fun2(string str)
{
Console.WriteLine(str + "非new"); }
static void Main(string[] args)
{
DelFunc del = new DelFunc(Fun1);
del += Fun2;
del += delegate(string str)
{
Console.WriteLine(str+"匿名方法");
};
del+=str=>Console.WriteLine(str+"lamada表达式");
del("赋值给委托变量,通过"); Console.ReadKey();
}
}
}

new,+=,delegate匿名方法,lamada表达式(就是方法,匿名的)

委托约束方法的 参数返回值,泛型约束参数返回值的类型。

 namespace ConsoleApplication1
{
//委托,规定返回值和参数,泛型<>,规定参数和返回值类型。
delegate T3 Del<T1, T2, T3>(T1 m, T2 n);//定义:只写T,不写具体的类型,<>里 输入和返回。( )参数,并没有返回值** class Program
{
public static string Delfun1(string str1, string str2)
{
Console.WriteLine();
return str1 + str2;
}
public static string Delfun2(string str1, string str2)
{
Console.WriteLine();
return str1 + str2 + "第二个方法";
}
static void Main(string[] args)
{
{
//new或=或+=时,指向的方法必须具体和其委托匹配的参数返回值 类型。
Del<string, string, string> DelEntity = new Del<string, string, string>(Delfun1);
DelEntity += Delfun2;
DelEntity += delegate(string str1, string str2) { Console.WriteLine(); ;return str1 + str2 + "第三个方法"; };
DelEntity += (string str1, string str2) => { Console.WriteLine(); return str1 + str2 + "第四个方法"; };
//最后调用,传具体和其委托匹配的参数值
Console.WriteLine(DelEntity("字符串1", "字符串2"));
Console.ReadKey();
}
}
}
}

泛型委托。写一个模仿的 delegate T2 Func(in T1,out T2)(T1 arg)

Func的超强分析

 using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
//Where Find OrderBy Take Skip //委托,规定返回值和参数,泛型<>,规定参数和返回值类型。
//delegate T3 Del<in T1, in T2, out T3>(T1 m, T2 n);//定义:只写T,不写具体的类型,<>里 输入和返回。( )参数加个in,并没有返回值加out**
//public delegate TResult Func<in T, out TResult>(T arg);
class Program
{
public static bool fun(string str)
{
if (str.Contains("aa"))
{
return true;
}
else
{
return false;
}
}
static void Main(string[] args)
{
List<string> list = new List<string>() { "aa", "bb", "dd" };//using System.Collections.Generic;
//public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
//where是IEnumerable<TSource>泛型接口的扩展(泛型)方法。 //public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
//List<>继承了IEnumerable<TSource>泛型接口,所以具有where方法。 //此扩展泛型方法存放的位置,是写在public static class Enumerable类里面 //当list<T>确定里面的元素类型,比如string,父类接口 IEnumerable<T>也会确定其类型,
//父类接口 IEnumerable<T>的扩展泛型方法where<T>其的参数Func<T,bool>,T也会确定其类型。
//一般where<T>的T可以省略。 //where(Func)中的Func是对这个集合的每一项( 每一项变量=>条件方法体)进行查询,符合条件方法体的返回true
//where方法返回这样多个元素就是IEnumerable<T>集合,用其接受,再foreach遍历。 //第一Func委托类型的一个委托变量对应的lamada表达式
//IEnumerable<string> temp = list.Where<string>(i => i.Contains("a"));//using System.Linq;
//第二Func委托类型的一个委托变量,事先已经指向定义好的函数
//Func<string, bool> fun11 = new Func<string, bool>(fun);
//第三Func委托类型的一个委托变量,让其已经指向一个匿名函数函数
//Func<string, bool> fun11 = delegate(string str)
//{
// if (str.Contains("aa"))
// {
// return true;
// }
// else
// return false;
//};
//第四Func委托类型的一个委托变量=一个函数。
Func<string, bool> fun11=fun;
IEnumerable<string> temp = list.Where<string>(fun11);
foreach (var item in temp)
{
Console.WriteLine(item);
}
Console.ReadKey(); //Where Find OrderBy Take Skip
//var res = list.Find(a => a.Equals("aa"));
//int[] arr = { 1, 2, 3 };
}
}
}

Where Find OrderBy Take Skip用到的泛型委托方法。Func的来龙去脉

T2 Func<in T1,out T2>(T1 arg)的更多相关文章

  1. 现在有T1、T2、T3三个线程,怎样保证T2在T1执行完后执行,T3在T2执行完后执行?使用Join

    public class TestJoin { public static void main(String[] args) { Thread t1 = new Thread(new T1(), &q ...

  2. 【测试】在hr用户下自行创建T1和T2表写一条SQL语句,(NL连接)

    SQL> select t1.* from t1,t2 where t1.object_id=t2.object_id; rows selected. Execution Plan ------ ...

  3. MRI中T1和T2的含义与区分[转]

    A. MRI名词解释   T1加权像.T2加权像为磁共振检查中报告中常提到的术语,很多非专业人士不明白是什么意思,要想认识何为T1加权像.T2加权像,请先了解几个基本概念:   1.磁共振(maget ...

  4. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group t1,customer t2

    ### SQL: select t1.gid,t1.gname,t1.gvalue,t1.gtype, t1.gaddress,t1.gmembers, t1.gcode,t1.gphone, t2. ...

  5. 查出了a表,然后对a表进行自查询,a表的别名t1,t2如同两张表,因为t1,t2查询的条件不一样,真的如同两张表,关联两张表,可以将两行或者多行数据合并成一行,不必使用wm_concat()函数。为了将t2表的数据全部查出来使用了右连接。

    with a as( select nsr.zgswj_dm, count(distinct nsr.djxh) cnt, 1 z from hx_fp.fp_ly fp, hx_dj.dj_nsrx ...

  6. T1,T2,T3 三个线程顺序执行

    T1,T2,T3 三个线程顺序执行 现在有 T1.T2.T3 三个线程,你怎样保证 T2 在 T1 执行完后执行,T3 在 T2 执行完后执行?(T1->T2->T3) 这个线程问题通常会 ...

  7. 现在有T1、T2、T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行?

    Thread t1 = new Thread(new T1()); Thread t2 = new Thread(new T2()); Thread t3 = new Thread(new T3()) ...

  8. T1加权像(T1 weighted image,T1WI)

    T1加权成像(T1-weighted imaging,T1WI)是指这种成像方法重点突出组织纵向弛豫差别,而尽量减少组织其他特性如横向弛豫等对图像的影响. 弛豫:物理用语,从某一个状态恢复到平衡态的过 ...

  9. Day2:T1搜索 T2最小生成树

    T1:广搜+判断矩形 注:如何判断搜的是否为矩形: 在广搜的时候,记录下边界的坐标,然后枚举一遍过去,如果搜到"."就是牛群,否则就是房间 瞥了一眼ccy的做法,据说是floodf ...

随机推荐

  1. C和指针 第八章 数组

    8.1 数组名和指针 int a; int b[10]; a称为一个标量,表示一个单一的值,变量的类型是整数. b是数组,b[1]的类型是整数,b是一个指针常量,表示数组第一个元素的地址.b的类型取决 ...

  2. PHP使用数组依次替换字符串中匹配项

    select * from table where ctime >= '[date-14]' and ctime <= '[date-1]'; 想把上面这句sql的中括号表示的日期依次换成 ...

  3. 使用git status快速commit

    提交之前使用git status可以看到将要提交的文件,如果想部分提交,需要单独commit.使用下面这句可以快速commit git commit `git status | grep 'mod' ...

  4. tornado 第二种路由方法(装饰器)

    #!/usr/bin/env python # _*_coding:utf-8 _*_ import tornado.ioloop import tornado.web application = t ...

  5. Ubuntu16.04 安装MATALAB R2015b教程

    1.安装 将镜像文件内文件解压出来,添加执行权限,否则执行 ./install指令会出错 chmod -R 777 MATALAB 执行如下指令 ./install 2.填入补丁内的密匙 在Matla ...

  6. mongoTemplate简单用法(增删改查)

    分页时查找数量: public long countSample(String id) { Query query = new Query(); if (StringUtil.hasText(id)) ...

  7. GNU/Linux下LVM配置管理以及快照卷、物理卷、卷组、逻辑卷的创建和删除

    LVM是Linux环境中对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上.文件系统之下的一个逻辑层,可提高磁盘分区管理的灵活性.最大的优点是在不损伤数据的前提下调整存储空间的大小. 本篇主要讲述L ...

  8. MyEclipse 10, 2013, 2014 破解、注册码

    MyEclipse 试用期限一般是三十天,过了三十天后 MyEclipse 会提示用户注册而不能正常使用,这里分享一下破解过程,仅供学习和参考. MyEclipse 10, 2013, 2014 破解 ...

  9. Spring实例化Bean的三种方式及Bean的类型

    1.使用类构造器实例化  [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...

  10. 深入Java核心 Java中多态的实现机制(1)

    在疯狂java中,多态是这样解释的: 多态:相同类型的变量,调用同一个方法时,呈现出多中不同的行为特征, 这就是多态. 加上下面的解释:(多态四小类:强制的,重载的,参数的和包含的) 同时, 还用人这 ...