Enum flags in C# are life
For the 5 years I’ve been working on and off with C# I’ve never encountered enum flags. Until yesterday. I thought the concept would make a great blog post so here it is!
Yesterday’s approach to enums
If you asked me to hold multiple enums in a variable here is how I would’ve done it before my enlightenment:
The new ways
Since we are learning every day here is how we could perform something similar with enum flags:
The nice thing about flags is that C# will automatically print out the name of all the enums instead of the number. Note that the values of enums are all powers of 2, ensuring that the number stored in a variable (in our case basket) is unique.
I hope this post was quite informative for you. Please remember to ABC (Always Be Coding)!