利用委托求任意数组的最大值

来源:互联网 发布:广寒仙子皎月淘宝价格 编辑:程序博客网 时间:2024/06/05 17:29

using System;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

 

namespace _05_利用委托求任意数组的最大值

{

   public delegate int DelGetMax(object o1,object o2);

   class Program

   {

       static void Main(string[] args)

       {

            object[] nums = { 123, 456, 789, 147, 258, 369 };

 

            object[] names = { "123", "sdsdsd", "456789", "qwertyuiop" };

 

            //求年龄最大人名

            object[] persons = { new Person() { Name = "张三", Age = 18 }, new Person() { Name = "李四", Age = 21 } };

 

            //objectmax = DelGetMax(nums, C1).ToString();

            //objectmax = DelGetMax(names, C2);

 

            //objectmax= DelGetMax(nums,delegate(object o1,object o2) { return (int)o1-(int)o2; });

            //objectmax = DelGetMax(nums,(o1,o2)=> { return (int)o1 - (int)o2; });

 

            //objectmax= DelGetMax(names, delegate (object o1, object o2) { return((string)o1).Length - ((string)o2).Length; });//匿名函数(函数的简写形式,只调用一次可以这样写)

            //objectmax = DelGetMax(names,(o1,o2)=> { return ((string)o1).Length -((string)o2).Length; });//lambda表达式(匿名函数的简写形式)

 

            //objectmax = DelGetMax(persons, delegate (object o1, object o2) { return((Person)o1).Age-((Person)o2).Age; });

            object max = DelGetMax(persons,(o1,o2)=>{return ((Person)o1).Age-((Person)o2).Age; });

 

 

            Console.WriteLine(((Person)max).Name);//输出年龄最大人的姓名

 

            Console.ReadKey();

       }

 

       ///<summary>

       ///普通方法求整数数组的最大值

       ///</summary>

       ///<param name="nums"></param>

       ///<returns></returns>

       static int GetMax(int[] nums)

       {

            //假设第一个元素最大

            int max = nums[0];

            for (int i = 0; i < nums.Length; i++)

            {

                if (nums[i] > max)

                {

                    max = nums[i];

                }

            }

            return max;

       }

 

       ///<summary>

       ///普通方法求字符串数组的最大值

       ///</summary>

       ///<param name="nums"></param>

       ///<returns></returns>

       static string GetMax(string[] nums)

       {

            string max = nums[0];

            for (int i = 0; i < nums.Length; i++)

            {

                if (nums[i].Length > max.Length)

                {

                    max = nums[i];

                }

            }

            return max;

       }

 

       ///<summary>

       ///利用委托求任意数组的最大值

       ///</summary>

       ///<param name="nums"></param>

       ///<returns></returns>

       static object DelGetMax(object[] nums, DelGetMax del)

       {

            object max = nums[0];

            for (int i = 0; i < nums.Length; i++)

            {

                //由于只有比较的方式不同,所以可以传一个比较的方式 即传一个委托进来

 

                if (del(max, nums[i]) < 0)

                {

                    max = nums[i];

                }

            }

            return max;

       }

 

       ///<summary>

       ///求整形最大值

       ///</summary>

       ///<param name="o1"></param>

       ///<param name="o2"></param>

       ///<returns></returns>

       static int C1(object o1, object o2)

       {

            int n1 = (int)o1;

            int n2 = (int)o2;

            return n1 - n2;

       }

 

       ///<summary>

       ///求字符串类型最大值

       ///</summary>

       ///<param name="o1"></param>

       ///<param name="o2"></param>

       ///<returns></returns>

       static int C2(object o1, object o2)

       {

            string s1 = (string)o1;

            string s2 = (string)o2;

            return s1.Length - s2.Length;

       }

   }

 

   ///<summary>

   ///创建一个Person类,利用委托求年龄最大人的姓名

   ///</summary>

   class Person

   {

       public string Name { get; set; }

       public int Age { get; set; }

   }

}

原创粉丝点击