gpt4 book ai didi

c# - 对象列表中对象参数的最大值

转载 作者:太空宇宙 更新时间:2023-11-03 20:48:18 24 4
gpt4 key购买 nike

<分区>

我想问一下,在 ListArray 中是否有一个简短的形式来查找某个对象列表的参数的最大值。

using System;
using System.Collections.Generic;

public class Program
{
public static void Main()
{
Car car1 = new Car(120);
Car car2 = new Car(140);
Car car3 = new Car(100);
List<Car> cars = new List<Car> { car1, car2, car3 };

// THIS I WANT TO SHORTEN ██████████
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
// Find car with maximum Power
double max = 0;
foreach (Car car in cars)
if (car.power > max)
max = car.power;
// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
// █████████████████████████████████

Console.WriteLine("The maximum power is = " + max);
}

public class Car
{
public double power; // Horsepower of the car
public Car (double power)
{
this.power = power;
}
}
}

我正在寻找一些简单的东西,比如 cars.findMax(car => car.power)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com