C
C#ā€¢2y 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ā€¢2y 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ā€¢2y 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ā€¢2y ago
Factory functions are a very valid pattern. Or do you mean something else?
Servator
Servatorā€¢2y ago
Everything is a valid pattern if it works šŸ˜„
Accord
Accordā€¢2y 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.
Want results from more Discord servers?
Add your server