任务:
(1)找出int数组里面最大值的那个元素
(2)找出string数组里面最长的那个元素
(3)找出Person数组里面年龄最大的那个元素的名字
一、下面代码是比较原始的做法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GetMaxExample
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 4, 5, 6, 7 };
string[] names = { "张", "李四", "王五明", "zhafdsfdsfds" };
Person[] persons = { new Person { Age = 12, Name = "小花" }, new Person { Age = 15, Name = "刘伟" }, new Person { Age = 22, Name = "东东" } };
int maxInt = 0;
string LengthName = string.Empty;
Person person = null;
Console.WriteLine(GetMaxInt(nums, maxInt));
Console.WriteLine(GetLengthString(names,LengthName));
Console.WriteLine(GetPersonName(persons,person));
}
#region 最原始的方式
//找出int数组里面最大值的那个元素
static int GetMaxInt(int[] nums,int max)
{
max = nums[0];
for (int i = 0; i < nums.Length; i++)
{
if (nums[i]>max)
{
max = nums[i];
}
}
return max;
}
//找出string数组里面最长的那个元素
static string GetLengthString(string[] names,string name)
{
name = names[0];
for (int i = 0; i < names.Length; i++)
{
if ((names[i].Length-name.Length)>0)
{
name = names[i];
}
}
return name;
}
//找出Person数组里面年龄最大的那个元素的名字
static string GetPersonName(Person[] persons,Person person)
{
person = persons[0];
for (int i = 0; i < persons.Length; i++)
{
if (persons[i].Age>person.Age)
{
person = persons[i];
}
}
return person.Name;
}
#endregion
static object
}
class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
}
二、统一用Ojbect类型来进行比较(涉及到拆箱和装箱,效率不高)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _02求数组的最大值
{
public delegate int DelCompare(object o1, object o2);
class Program
{
static void Main(string[] args)
{
object[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
object[] names = { "张三", "李四", "王五", "zhafdsfdsfds" };
object[] pers = { new Person() { Name = "张三", Age = 19 }, new Person() { Name = "李四", Age = 29 } };
#region 整型数组比较
DelCompare delComInt = new DelCompare(C1);
object maxNum = GetMax(nums, delComInt);
Console.WriteLine((int)maxNum);
#endregion
#region 字符串数组比较
DelCompare delComString = new DelCompare(C2);
object lengthName = GetMax(names, delComString);
Console.WriteLine((string)lengthName);
#endregion
#region Person数组比较
//正常使用委托
DelCompare delComPerson = new DelCompare(C3);
object maxAgeName = GetMax(pers, delComPerson);
//匿名函数
//object maxAgeName = GetMax(pers, delegate(object o1, object o2) { return ((Person)o1).Age - ((Person)o2).Age; });
//Lambda表达式
//object maxAgeName = GetMax(pers, (o1, o2) => { return ((Person)o1).Age - ((Person)o2).Age; });
Console.WriteLine(((Person)maxAgeName).Name);
#endregion
}
static object GetMax(object[] array, DelCompare del)
{
object max = array[0];
for (int i = 0; i < array.Length; i++)
{
if (del(max, array[i]) < 0)//需要传进来一个比较的方式
{
max = array[i];
}
}
return max;
}
static int C1(object o1, object o2)
{
int n1 = (int)o1;
int n2 = (int)o2;
return n1 - n2;
}
static int C2(object o1, object o2)
{
string s1 = (string)o1;
string s2 = (string)o2;
return s1.Length - s2.Length;
}
static int C3(object o1,object o2)
{
Person p1 = (Person)o1;
Person p2 = (Person)o2;
return p1.Age - p2.Age;
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
三、使用泛型委托和Lambda表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TDelegateExample
{
class Program
{
public delegate int DelCompare<T>(T T1, T T2);
static void Main(string[] args)
{
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8,9 };
string[] names = { "agbaef", "atetq", "tiuqehlasd" };
Person[] persons = { new Person() { Name = "张三", Age = 19 }, new Person() { Name = "李四", Age = 29 } };
//泛型委托 Lambda表达式
int numResult = GetMax(nums, (num1, num2) => { return num1 - num2; });
Console.WriteLine(numResult);
string stringResult = GetMax(names, (string1, string2) => { return string1.Length - string2.Length; });
Console.WriteLine(stringResult);
Person personResult = GetMax(persons, (person1, person2) => { return person1.Age - person2.Age; });
Console.WriteLine(((Person)personResult).Name);
}
static T GetMax<T>(T[] array,DelCompare<T> del)
{
T max = array[0];
for (int i = 0; i < array.Length; i++)
{
if (del(max,array[i])<0)
{
max = array[i];
}
}
return max;
}
}
class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
}