using System;using System.Collections.Generic;using System.Linq;classProgram{staticvoidMain(){
List<int> numbers =newList<int>{1,2,3,4,5,6,7,8,9,10};// 偶数のみを取得var evenNumbers = numbers.Where(n => n %2==0);
Console.WriteLine("偶数:");foreach(var num in evenNumbers){
Console.WriteLine(num);}// 数値を2倍にするvar doubledNumbers = numbers.Select(n => n *2);
Console.WriteLine("\n2倍にした数:");foreach(var num in doubledNumbers){
Console.WriteLine(num);}// 5より大きい数の合計を計算int sum = numbers.Where(n => n >5).Sum();
Console.WriteLine($"\n5より大きい数の合計: {sum}");// 数値を降順にソートvar sortedNumbers = numbers.OrderByDescending(n => n);
Console.WriteLine("\n降順にソートした数:");foreach(var num in sortedNumbers){
Console.WriteLine(num);}}}
コメント