types and references - Getting the reference - Array, index
Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 4(types and references - Getting the reference)、p.202(Sharpen your pencil)の解答を求めてみる。
コード
Program.cs
using System;
namespace MyFirstConsoleApp
{
class Program
{
static void Main(string[] args)
{
Elephant[] elepahnts = new Elephant[7];
elepahnts[0] = new Elephant() { Name = "Lloyd", EarSize = 40 };
elepahnts[1] = new Elephant() { Name = "Lucinda", EarSize = 33 };
elepahnts[2] = new Elephant() { Name = "Larry", EarSize = 42 };
elepahnts[3] = new Elephant() { Name = "Lucille", EarSize = 32 };
elepahnts[4] = new Elephant() { Name = "Lars", EarSize = 44 };
elepahnts[5] = new Elephant() { Name = "Linda", EarSize = 37 };
elepahnts[6] = new Elephant() { Name = "Humphery", EarSize = 45 };
int[] sizes = {0, 40, 42, 42, 44, 44, 45 };
Elephant biggestEars = elepahnts[0];
for (int i = 1; i < elepahnts.Length; i++)
{
Console.WriteLine($"Iteration #{i}");
if (elepahnts[i].EarSize > biggestEars.EarSize)
{
biggestEars = elepahnts[i];
}
Console.WriteLine(biggestEars.EarSize == sizes[i]);
}
}
}
}
入出力結果(Terminal, Zsh, csi(C# Interactive))
Iteration #1
True
Iteration #2
True
Iteration #3
True
Iteration #4
True
Iteration #5
True
Iteration #6
True