Petkov Alexander
Petkov Alexander

Reputation: 65

Is it possible to create an instance of a class with "new class" key words ? c#

I have an array filled with the names if some classes in my program .

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

is it possible to create an instance of a class like this:

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

Upvotes: 1

Views: 70

Answers (1)

Sweeper
Sweeper

Reputation: 273465

Maybe you are looking for object initialisers?

Assuming you have a class Pokemon:

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

and a List<Pokemon> called list, you can do this:

list.Add(new Pokemon { Name = "sdfs", Type = "sfds", Age = 123 });

Upvotes: 1

Related Questions