I've trying to work with a struct (for the first time) and have come up with a slight issue I don't know how to work around.
public struct TextSelect<TEnum> where TEnum : Enum
{
public TextSelect(string input = "")
{
Input = input;
Values = EnumDto.ToList<TEnum>();
}
public string Input { get; set; }
public IEnumerable<EnumDto> Values { get; }
}
public TextSelect<IndustryType> Industry = new TextSelect<IndustryType>();
The problem relates to parameterless constructors.
I would like to initialize the Values
property when the struct is instantiated, but TEnum
is a type, not an instance value, so it doesn't count as a parameter.
As such, I get an compiler warning if the constructor has no parameters.
If I add an optional value, 'Input', I can trick the compiler and warning disappears, but the constructor still doesn't fire (presumably because it's empty).
Other than change it to a class, are there any other workarounds?
All advice appreciated.
IEnumerable<T>
,string
), the heap is involved anyhow. – Heinz Kesslerset
on thisstruct
already tells me that you're in very very dangerous water here. Why are you trying to make this as astruct
? – Marc Gravellnew TextSelect()
(ordefault
), the implicit default initializer (just an "all zero" blit) will be a better match than using the optional parameter, so the default initializer will be used. – Marc Gravell