1. 行为函数委托(Action(T))
    封装一个方法,该方法只采用一个参数并且不返回值。
    结构:void 方法名(T obj){//处理};
    例:
        using System;
        
    using System.Collections.Generic;
        
    class Program
        {
            
    public string Name { getset; }
            
    public void Print(Action<string> action)
            {
                action(
    this.Name);
            }
            
    static void Main(string[] args)
            {
                Console.WriteLine(
    "List<T>中使用");
                List
    <string> list = new List<string> { "Flash""Past""Lian""Xi" };
                list.ForEach(Program.Print);
                Console.WriteLine(
    "自定义方法使用");
                Program pro 
    = new Program { Name = "FlashPast" };
                pro.Print(Program.Print);
                Console.ReadLine();
            }
            
    public static void Print(string obj)
            {
                Console.WriteLine(obj);
            }
        }

  2. 判定函数委托(Predicate(T))
    表示定义一组条件并确定指定对象是否符合这些条件的方法。
    结构:bool Match<T>(T obj){//处理}
        using System;
        
    using System.Collections.Generic;
        
    class Program
        {
            
    public string Name { getset; }
            
    static void Main(string[] args)
            {
                Console.WriteLine(
    "List<T>中使用");
                Program pro 
    = new Program { Name = "Flash" };
                List
    <string> list = new List<string> { "Flash""Past""Lian""Xi" };
                
    string result = list.Find(pro.Match);
                Console.WriteLine(result);
                Console.ReadLine();
            }
            
    public bool Match(string obj)
            {
                
    bool result = false;
                
    if (obj == this.Name)
                {
                    result 
    = true;
                }
                
    return result;
            }
        }

  3. 转换函数委托(Converter(TInput,TOutput))
    表示将对象从一种类型转换为另一种类型的方法。
    结构: TOutput Converter<TInput,TOutput>(TInput input){//处理}
        using System;
        
    using System.Collections.Generic;
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Console.WriteLine(
    "List<T>中使用");
                List
    <string> stringList = new List<string> { "1""2""3""4" };
                List
    <int> intList = stringList.ConvertAll<int>(Program.Converter);
                intList.ForEach(
    delegate(int obj) { Console.WriteLine(obj.ToString()); });
                Console.ReadLine();
            }
            
    public static int Converter(string input)
            {
                
    int result;
                
    if (!int.TryParse(input, out result))
                {
                    result 
    = -1;
                }
                
    return result;
            }
        }

  4. 比较函数委托(Comparison(T))
    表示比较同一类型的两个对象的方法。
    结构: int Comparison<T>(T x,T y){//处理}
        using System;
        
    using System.Collections.Generic;
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Console.WriteLine(
    "List<T>中使用");
                List
    <int> list = new List<int> { 1254637288 };
                list.ForEach(
    delegate(int obj) { Console.WriteLine(obj); });
                list.Sort(Program.Comparison);
                list.ForEach(
    delegate(int obj) { Console.WriteLine(obj); });
                Console.ReadLine();
            }
            
    public static int Comparison(int x, int y)
            {
                
    return x.CompareTo(y);
            }
        }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2021-07-06
  • 2021-06-19
  • 2021-12-28
  • 2022-03-06
猜你喜欢
  • 2021-06-15
  • 2022-12-23
  • 2021-11-21
  • 2022-01-05
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案