gpt4 book ai didi

c# - 使用 NameValueCollection 类存储两个以上的值

转载 作者:可可西里 更新时间:2023-11-01 11:35:56 25 4
gpt4 key购买 nike

我有一组字符串需要存储在一个集合中,比如:

身份证、名字、姓氏、城市、国家、语言

以上都适用于一个人(用ID表示)

现在我有 60 到 70 个(并且还在增加),我该如何组织它们?我查看了 NameValueCollection 类 - 它完全符合我的要求(如果我只有两个字段),但由于我有 6 个字段,所以我无法使用它。例如:

public NameValueCollection personCollection = new NameValueCollection
{
{ "harry", "townsend", "london", "UK", "english" },
{ "john", "cowen", "liverpool", "UK", "english" },
// and so on...
};

虽然这行不通:(有人可以建议另一种实现此目的的方法吗?

最佳答案

用你需要的属性创建一个 Person 类怎么样?

 public class Person
{
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
// more attributes here
}

然后,只需实例化 Person 类并创建新的 Person 对象。然后,您可以将这些人添加到列表中。

        Person somePerson = new Person();
somePerson.firstname = "John";
somePerson.lastname = "Doe";
somePerson.id = 1;

List<Person> listOfPersons = new List<Person>();
listOfPersons.Add(somePerson);

关于c# - 使用 NameValueCollection 类存储两个以上的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11063549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com