泛型委托

using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
    public delegate int DelegateOne<T>(T a, T b);

    internal class Program
    {
        public static void Main(string[] args)
        {
            string[] ints = {"aff","z","fdsd","aaaaaa","yes"};
            var max = GetMax(ints, delegate(string a, string b)
            {
                return a.Length - b.Length;
            });
//            int[] ints = {3, 2, 54, 23, 222, 9999};
//            var max = GetMax(ints, (o, oo) => o.CompareTo(oo));
            Console.WriteLine(max);
        }

        static T GetMax<T>(T[]arr, DelegateOne<T> del)
        {
            T max = arr[0];
            foreach (T t in arr)
            {
                if (del(max, t) < 0)
                {
                    max = t;
                }
            }
            return max;
        }
    }
}

 

posted on 2017-10-03 20:17  靖康耻  阅读(100)  评论(0编辑  收藏  举报

导航