T2 Func (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的來龍去脈

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM