C
C#ā€¢16mo ago
caiosa2339

ā” How to add a list of type of another class in c#?

I'm doing a program where I need to store the a list of the Person class in the class of People. A Person is defined by her name, and age...How do I store a list of Person inside the People class?
5 Replies
Angius
Angiusā€¢16mo ago
It seems more like you want a list of people, not necessarily a People class with a list Anyway, you do that like you do with any other property
class People
{
public List<Person> People { get; set; }
}
class People
{
public List<Person> People { get; set; }
}
Servator
Servatorā€¢16mo ago
You could initialize a List<Person> with static method Then you could add person to that list in ctor but its hacky
Anton
Antonā€¢16mo ago
Factory functions are a very valid pattern. Or do you mean something else?
Servator
Servatorā€¢16mo ago
Everything is a valid pattern if it works šŸ˜„
Accord
Accordā€¢16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.