是否可以使用“新类”关键字创建类的实例? C#

时间:2019-02-24 17:01:15

标签: c#

如果程序中有某些类,我将使用名称填充数组。

string[] arr= new string[] {"company", "pokemon", "parents"}

是否可以创建这样的类的实例:

list.Add(new class( arr[0] { "sdfs", "sfds", 123} ))

1 个答案:

答案 0 :(得分:1)

也许您正在寻找对象初始化程序

假设您有Pokemon类:

class Pokemon {
    public string Name { get; set; }
    public string Type { get; set; }
    public int Age { get; set; }
}

和名为List<Pokemon>的{​​{1}},您可以执行以下操作:

list