新しいクラスを絶対に作成したくない場合は、ID をキーにしたリストの辞書を使用できます。
IDictionary<string, IList<string>> personCollection =
new Dictionary<string, IList<string>>
{
{ "1", new [] { "harry", "townsend", "london", "UK", "english" }},
{ "2", new [] { "john", "cowen", "liverpool", "UK", "english" }},
};
…辞書とリストのインデクサーを使用してアクセスできます。
Console.WriteLine(personCollection["1"][0]); // Output: "harry"
Console.WriteLine(personCollection["2"][2]); // Output: "liverpool"
ただし、正しい OOP アプローチは、それぞれの文字列のプロパティを持つクラスを定義することです。
public class Person
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Language { get; set; }
public Person() { }
public Person(string id, string firstName, string lastName,
string city, string country, string language)
{
this.Id = id;
this.FirstName = firstName;
this.LastName = lastName;
this.City = city;
this.Country = country;
this.Language = language;
}
}
次に、人のリストを作成できます。
IList<Person> persons = new List<Person>()
{
new Person("1", "harry", "townsend", "london", "UK", "english"),
new Person("2", "john", "cowen", "liverpool", "UK", "english"),
};