Reputation: 65
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
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