我有一个有对象的数组,这些对象有一个字符串,一个整数和一个char属性.
Person[] people = new Person[1]; //Declare the array of objects
[...] This code is irrelevant
Person person = new Person(name, age, gender); //This creates instance a new object
people.SetValue(person, i); //is just a variable which increase in a c
Array.Resize(ref people, people.Length + 1); //Change array size
i++; // Autoincrement
Run Code Online (Sandbox Code Playgroud)
[...]更多代码来填充有效的值
从人数组中存储的所有对象中,我想得到人的年龄最大值(年龄属性是整数值)
最简单的方法是使用LINQ:
using System.Linq;
var maxVal = people.Max(x => x.Age); // get highest age
var person = people.First(x => x.Age == maxVal); // get someone with such an age
Run Code Online (Sandbox Code Playgroud)