I have some code that sets up a dictionary with some defualt values for an enum:
foreach (string status in Enum.GetNames(typeof(TargetStatusType)))
{
dict.Add((TargetStatusType)Enum.Parse(typeof(TargetStatusType), status), 0);
}
Is there an easier way to do this, as it seems a bit messy.
I was hoping I could just do
foreach(TargetStatusType status in ?) ...
Thanks!
Use
Enum.GetValues():Note that although
GetValues()is only declared to returnArray, theforeachloop performs the cast for you automatically. Alternatively you could cast the result toTargetStatusType[]if you wanted to use it in a different loop where casting each value would be inconvenient.