I have a struct
that contains two lists:
struct MonthData
{
public List<DataRow> Frontline;
public List<DataRow> Leadership;
}
However, I want to initialize both when the struct is created. If I try:
struct MonthData
{
public List<DataRow> Frontline = new List<DataRow>();
public List<DataRow> Leadership = new List<DataRow>();
}
Then I get:
Error 23 'MonthData.Frontline': cannot have instance field initializers in structs
...
Since structs cannot have parameterless constructors, I can't just set this in a constructor either. So far, I can only see the following options:
What is the recommended approach for this? Right now, I'm thinking making this a class is the best idea.
You're using reference types (List<T>
) in your struct anyway, thus the usage of a struct as value type wouldn't make any sense to me. I'd just go with a class.
You ought to use a class instead. From MSDN:
In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With