zrfv8q
Last Updated: February 25, 2016
·
936
· keboo

Toggle an enum in .NET

I created an enum to keep track of two states within my object. Sure I could have just used a boolean but the enum seemed to make the code easier to read.

private enum SomeEnum : byte
{
    FirstValue = 0,
    SecondValue = 1
}

private SomeEnum _value;

public void ToggleValue()
{
    _value ^= (SomeEnum)1;
    //Alternatively
    //_value = (SomeEnum)(((int)_value + 1) % 2);
}
Say Thanks
Respond