I have this code, but it looks a bit verbose:
enum MonitoringMode
{
Seek,
Destroy
}
void ToggleMonitoringMode()
{
if (_monitoringMode == MonitoringMode.Seek)
_monitoringMode = MonitoringMode.Destroy;
else
if (_monitoringMode == MonitoringMode.Destroy)
_monitoringMode = MonitoringMode.Seek;
}
There are some well known idioms to toggle between two values, like:
bitwiseBoolean ^= bitwiseBoolean; // bit toggle boolean = !boolean; // boolean toggle oneOrZero = 1 - oneOrZero; // numeric toggle
Is there a similar, less verbose way to get the same result? I am not sure using the int
value of enums would be safe or even "elegant" in C#.
bool InDestroyMode =true;
InDestroyMode = !InDestroyMode
is a lot simpler than the accepted answer. – Kevin Aug 27 '15 at 20:01