I have the following enum
in my code:
[Flags]
public enum Column
{
FirstName = 1 << 0,
LastName = 1 << 1,
Address = 1 << 2,
}
Now I have a method that can take a enum
.
public void ParseColumns(Column col)
{
//do some parsing...
}
This method can be called like this:
ParseColumns(Column.FirstName | Column.LastName);
ParseColumns(Column.LastName);
ParseColumns(Column.LastName | Column.Address | Column.FirstName);
ParseColumns(Column.Address | Column.FirstName);
I now need to iterate through the values but keep the order in which the enum
was passed to the methods.
I have found the following method which gave me the possibility to iterate through them, but sadly it returns the Order in which its defined in the Enum
itself and not how I called the method.
Iterate over values in Flags Enum?
I now need to iterate through the values but keep the order in which the enum was passed to the methods.
There's no such concept. The |
operator doesn't have any way of preserving ordering. To put it another way, imagine we had:
public void Foo(int x)
and you called it with:
Foo(1 + 4)
Foo(2 + 3)
How would you expect it to differentiate? If you need to pass separate values in, with a specific preserved order, you should probably use:
public void ParseColumns(params Column[] columns)
... at which point you may want to avoid it being a flags enum at all.
See more on this question at Stackoverflow