zoukankan      html  css  js  c++  java
  • 泛型函数Func<>

     
    泛型函数,即可以接受任何类型的通用函数(有where约束除外)。 
    例如:调用GetCacheData<int>,那第二个委托参数就是Func<int>,这个函数返回值也是int
    调用GetCacheData<string>,那第二个委托参数就是Func<string>,这个函数返回值是string
     
    public static int StrToInt(String s){
        return int.Parset(s);
    }
    
    public static void Main(String[] argv){ 
        String[] s = { "123", "345", "567" };            
        IEnumerable<int> t=s.Select<String, int>(StrToInt);            
        foreach (int i in t)           
         {                
            Console.WriteLine(i);            
         }
    }        
    微软帮助用户实现了一些内置的泛型类,泛型委托等,如Func<T>,Func<T,T>...,Tuple<T>,Tuple<T,T>...,Action<T>....仅仅只是方便用户的使用而已,例如上面的例子,利用Func<T>可以使得这个函数GetCacheData搜索<T>接受任意单返回值无参数的委托。所以总体说,只是为了方便开发者,不需要再去定义了而已。
    
    (1).用Func<T>的话
    public static void Main(string[] args)
            {
                String[] s = { "123", "456", "789" };
                Func<string, int> f = (x) => int.Parse(x);
                IEnumerable<int> t = s.Select<String, int>(f);
                foreach (int i in t)
                {
                    Console.WriteLine(i);
                }
    
                //Console.WriteLine("hello wor!");
                Console.ReadLine();
            }
  • 相关阅读:
    jedis操作redis事务练习
    jedis连接redis,测试操作redis常用的数据类型 String List Set Hash Zset
    多态的理解
    binarySeach方法
    数组重新认识
    String的 认识
    接口的 认识
    抽象类及抽象方法
    protected的深刻理解
    protected的认识
  • 原文地址:https://www.cnblogs.com/iDennis/p/5331735.html
Copyright © 2011-2022 走看看