by Wojciech Sura

In operator in C#?

Most languages provides a very useful in operator, which tests, whether item is in specific collection, for instance, in Delphi:

[delphi]
if state in [State1, State2, State3] then Writeln("State 1-3!");
[/delphi]

C# unfortunately lacks this operator, but we may cheat a little and reverse the condition:

[csharp]
if (new[] { MyStates.State1, MyStates.State2, MyStates.State3 }.Contains(state))
Console.WriteLine("State 1-3!");
[/csharp]

It is decision of developer, whether this notation is clear enough. The alternative is either test different conditions:

[csharp]
if (state == MyStates.State1 || state == MyStates.State2 || state == MyStates.State3)
Console.WriteLine("State 1-3!");
[/csharp]

Or you may store the required states in some temporary variable:

[csharp]
var states = new[] { MyStates.State1, MyStates.State2, MyStates.State3 };
if (states.Contains(state))
Console.WriteLine("State 1-3!");
[/csharp]

The first option seems the most elegant to me though.