T2 Func<in T1,out T2>(T1 arg)

委托调用方法的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表达式(就是方法,匿名的)

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

 1 namespace ConsoleApplication1
 2 {
 3     //委托,规定返回值和参数,泛型<>,规定参数和返回值类型。
 4     delegate T3 Del<T1, T2, T3>(T1 m, T2 n);//定义:只写T,不写具体的类型,<>里 输入和返回。( )参数,并没有返回值**
 5 
 6     class Program
 7     {
 8         public static string Delfun1(string str1, string str2)
 9         {
10             Console.WriteLine(1);
11             return str1 + str2;
12         }
13         public static string Delfun2(string str1, string str2)
14         {
15             Console.WriteLine(2);
16             return str1 + str2 + "第二个方法";
17         }
18         static void Main(string[] args)
19         {
20             {
21                 //new或=或+=时,指向的方法必须具体和其委托匹配的参数返回值 类型。
22                 Del<string, string, string> DelEntity = new Del<string, string, string>(Delfun1);
23                 DelEntity += Delfun2;
24                 DelEntity += delegate(string str1, string str2) { Console.WriteLine(3); ;return str1 + str2 + "第三个方法"; };
25                 DelEntity += (string str1, string str2) => { Console.WriteLine(3); return str1 + str2 + "第四个方法"; };
26                 //最后调用,传具体和其委托匹配的参数值
27                 Console.WriteLine(DelEntity("字符串1", "字符串2"));
28                 Console.ReadKey();
29             }
30         }
31     }
32 }
泛型委托。写一个模仿的 delegate T2 Func(in T1,out T2)(T1 arg)

Func的超强分析

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 namespace ConsoleApplication1
 5 {
 6     //Where  Find  OrderBy Take Skip
 7 
 8     //委托,规定返回值和参数,泛型<>,规定参数和返回值类型。
 9     //delegate T3 Del<in T1, in T2, out T3>(T1 m, T2 n);//定义:只写T,不写具体的类型,<>里 输入和返回。( )参数加个in,并没有返回值加out**
10     //public delegate TResult Func<in T, out TResult>(T arg);
11     class Program
12     {
13         public static bool fun(string str)
14         {
15             if (str.Contains("aa"))
16             {
17                 return true;
18             }
19             else
20             {
21                 return false;
22             }
23         }
24         static void Main(string[] args)
25         {
26             List<string> list = new List<string>() { "aa", "bb", "dd" };//using System.Collections.Generic;
27             //public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
28             //where是IEnumerable<TSource>泛型接口的扩展(泛型)方法。
29 
30             //public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
31             //List<>继承了IEnumerable<TSource>泛型接口,所以具有where方法。
32 
33             //此扩展泛型方法存放的位置,是写在public static class Enumerable类里面
34 
35             //当list<T>确定里面的元素类型,比如string,父类接口 IEnumerable<T>也会确定其类型,
36             //父类接口 IEnumerable<T>的扩展泛型方法where<T>其的参数Func<T,bool>,T也会确定其类型。
37             //一般where<T>的T可以省略。
38 
39             //where(Func)中的Func是对这个集合的每一项( 每一项变量=>条件方法体)进行查询,符合条件方法体的返回true
40             //where方法返回这样多个元素就是IEnumerable<T>集合,用其接受,再foreach遍历。
41 
42             //第一Func委托类型的一个委托变量对应的lamada表达式
43             //IEnumerable<string> temp = list.Where<string>(i => i.Contains("a"));//using System.Linq; 
44             //第二Func委托类型的一个委托变量,事先已经指向定义好的函数
45             //Func<string, bool> fun11 = new Func<string, bool>(fun);
46             //第三Func委托类型的一个委托变量,让其已经指向一个匿名函数函数
47             //Func<string, bool> fun11 = delegate(string str)
48             //{
49             //    if (str.Contains("aa"))
50             //    {
51             //        return true;
52             //    }
53             //    else
54             //        return false;
55             //};
56             //第四Func委托类型的一个委托变量=一个函数。
57             Func<string, bool> fun11=fun;
58             IEnumerable<string> temp = list.Where<string>(fun11);
59             foreach (var item in temp)
60             {
61                 Console.WriteLine(item);
62             }
63             Console.ReadKey();
64 
65             //Where  Find  OrderBy Take Skip
66             //var res = list.Find(a => a.Equals("aa"));
67             //int[] arr = { 1, 2, 3 };
68         }
69     }
70 }
Where Find OrderBy Take Skip用到的泛型委托方法。Func的来龙去脉

   

原文地址:https://www.cnblogs.com/leee/p/4456840.html