在C#中的List中保存不同类型的数据

sar*_*own 1 c# printing

我有几个数据,如姓名,身份证,年龄,地址,电话.每次用户输入数据时,它都会保存到List<>.我正在使用List<>每个数据.还有其他选择我只能使用一个List<>.哪个可以保存所有数据?

这是我的代码.

List<String> list1 = new List<String>();
                list1.Add(name);
List<String> list2 = new List<String>();
                list2.Add(ID);
List<String> list3 = new List<String>();
                list3.Add(age);
List<String> list4 = new List<String>();
                list4.Add(address);
List<String> list5 = new List<String>();
                list5.Add(phone);

for (int a = 0; a < list.Count; a++) // Loop through List with for
{                        
    listBox1.Items.Add(list1[i]);
}
for (int a = 0; a < list.Count; a++) // Loop through List with for
{                        
    listBox2.Items.Add(list2[i]);
}
for (int a = 0; a < list.Count; a++) // Loop through List with for
{                        
    listBox3.Items.Add(list3[i]);
}
for (int a = 0; a < list.Count; a++) // Loop through List with for
{                        
    listBox4.Items.Add(list4[i]);
}
for (int a = 0; a < list.Count; a++) // Loop through List with for
{                        
    listBox5.Items.Add(list5[i]);
}
Run Code Online (Sandbox Code Playgroud)

我还想过使用listbox来打印数据.我的另一种选择是只在一个列表框中打印出每个数据.

Max*_*ich 7

当然,宣布一个这样的类......

public class Person
{
    public Guid   ID      { get; set; }
    public string Name    { get; set; }
    public int    Age     { get; set; }
    public string Address { get; set; }
    public string Phone   { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它..

List<Person> personList = new List<Person>();
personList.Add(new Person { Name    = "Max", 
                            ID      = Guid.NewGuid, 
                            Address = "Unicorn Lane, Unicorn World", 
                            Age     = 26, 
                            Phone   = "123456" });
Run Code Online (Sandbox Code Playgroud)