• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
flashpast
博客园    首页    新随笔    联系   管理    订阅  订阅

常用的函数委托类型

  1. 行为函数委托(Action(T))
    封装一个方法,该方法只采用一个参数并且不返回值。
    结构:void 方法名(T obj){//处理};
    例:
        using System;
        
    using System.Collections.Generic;
        
    class Program
        {
            
    public string Name { get; set; }
            
    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 { get; set; }
            
    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> { 12, 5, 46, 37, 28, 8 };
                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);
            }
        }
posted @ 2009-10-20 13:45  flashpast  阅读(470)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3