如果您绝对不想创建任何新类,则可以使用由您的 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"),
};