LINQ and lambdas - Get control of your data - Enumerable, methods(Range, Sum, Average, Min, Max, Count, Last, Skip, Reverse)
Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 9(LINQ and lambdas - Get control of your data)、p.473(Sharpen your pencil)の解答を求めてみる。
コード
Program.cs
// 1 + 2 + 3 + 4 + 5 = 15
Console.WriteLine(Enumerable.Range(1, 5).Sum() == 15);
// (1 + 2 + 3 + 4 + 5 + 6) / 6 = 21 / 6 = 3.5
Console.WriteLine(Enumerable.Range(1, 6).Average() == 3.5);
Console.WriteLine(new int[] { 3, 7, 9, 1, 10, 2, -3 }.Min() == -3);
Console.WriteLine(new int[] { 8, 6, 7, 5, 3, 0, 9 }.Max() == 9);
Console.WriteLine(Enumerable.Range(10, 3721).Count() == 3721);
Console.WriteLine(Enumerable.Range(5, 100).Last() == 104);
// 9 + 6 + 2 = 17
Console.WriteLine(new List<int>() { 3, 8, 7, 6, 9, 6, 2 }.Skip(4).Sum() == 17);
Console.WriteLine(Enumerable.Range(10, 731).Reverse().Last() == 10);
入出力結果(Terminal, Zsh)
% dotnet run
True
True
True
True
True
True
True
True
%