計算機科学のブログ

enums and collections - Organizing your data - List, methods

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 8(enums and collections - Organizing your data)、p.423(Code Manage)の解答を求めてみる。

コード

Program.cs

string zilch = "zero";
string first = "one";
string second = "two";
string third = "three";
string fourth = "4.2";
string twopointtwo = "2.2";

List<string> a = new List<string>();
a.Add(zilch);
a.Add(first);
a.Add(second);
a.Add(third);
if (a.Contains("three"))
{
    a.Add("four");
}
a.RemoveAt(2);
if (a.IndexOf("four") != 4)
{
    a.Add(fourth);
}
if (a.Contains("two"))
{
    a.Add(twopointtwo);
}
PppPppL(a);
static void PppPppL(List<string> a)
{
    foreach (string element in a)
    {
        Console.WriteLine(element);
    }
}

入出力結果(Terminal, Zsh)

% dotnet run
zero
one
three
four
4.2
%