I have been working on a project recently that involves a lot of work with Flags Enums. To aid with this I created a set of Extension Methods:
Now the last 3 methods I am happy with - they are self explanatory and do what's expected. The first four however I am less convinced by.
My main problem is how I wrote some code:
Which to my surprise always returned
States.Blank rather than
Disconnected or a combination of the two. After a lot of close looking, I realised it was because the
Add method was a function and I was not using the return value.
The logical thing seemed to be changing the extension methods to use a reference parameter rather than a value parameter. While this worked in my vb.net library, the second I tried to use it in my C# test project (MSpec), it broke with the following error:
Error Argument 1 must be passed with the 'ref' keyword
So it cannot work like this, I have to return the result as a new instance of the enum. I don't like it, but other Structure based code (such as DateTime, String) work like this too.
On the point of mutability, I think a system like Ruby's of indicating a destructive method would be good:
But for now I will settle for returning a new instance.