using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
// 使用 Select 方法对每个元素进行操作,这里我们将每个数字乘以 2
var doubledNumbers = numbers.Select(x => x * 2);
// 输出结果
foreach (var num in doubledNumbers)
{
Console.WriteLine(num);
}
}
}
Select 是 LINQ 中的一个方法,用于对集合中的每个元素应用转换函数,并返回一个新的集合。numbers,然后使用 Select 方法将每个元素乘以 2。Select(x => x * 2) 表示对 numbers 列表中的每个元素 x 进行乘以 2 的操作。上一篇:c# out ref
下一篇:c# 向下取整
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站