There is a switch block for example:

private int State switch (State) { case -1: какие то операции case 0>: ?????? 

Swears at more zero, please tell me how to organize.

  • An interesting question, but probably so impossible - Valeriy Karchov
  • one
    I do not know, as in C #, but in other C-like languages ​​in case there can be only integer values ​​and nothing else. - skegg
  • and in Sharpe there are also lines - Valeriy Karchov

3 answers 3

Try using int.CompareTo :

 int result = State.CompareTo(0); switch (result) { case 1 : //>0 break; default: //все остальное break; } 
  • those. if my result has a value of 500 for example, will case 1 be executed? - Merlin
  • one
    result will not contain 500. CompareTo returns 1, 0 or -1 - ganouver
  • Got it. Thank! - Merlin
  • one
    @ganouver I agree, but only for those cases when a comparison with one value and three return values ​​is enough (more-equal-less) :) - barber
  • @barber so kind of requested. - ganouver

If you are not satisfied with the if statements, I would suggest something like this:

 enum ComparisonResult { IS_MINUS_ONE, GREATER_THAN_ZERO, OTHER_CASE } ComparisonResult result = ComparisonResult.OTHER_CASE; result = (state == -1) ? ComparisonResult.IS_MINUS_ONE : result; result = (state > 0) ? ComparisonResult.GREATER_THAN_ZERO : result; switch(result) { case ComparisonResult.IS_MINUS_ONE: // ... case ComparisonResult.GREATER_THAN_ZERO: // ... case ComparisonResult.OTHER_CASE: break; } 
  • one
    Option by the way, but this is ComparisonResult result = ComparisonResult.OTHER_CASE; result = (state == -1)? ComparisonResult.IS_MINUS_ONE: result; result = (state> 0)? ComparisonResult.GREATER_THAN_ZERO: result; You can make in the property. - Valeriy Karchov
  • As a fun solution, yes, I put +, but for practical use - a terrible perversion. - skegg
  • Yes ... of 2 lines in if such a sheet !!! C #? Melkosoft, I see that they have such hefty programs. - avp
  • it means that you can see the curves of the program> GREATER_THAN_ZERO never write anything other than constants to the caps! GreaterThenZero looks much better - Specter
  • @Valeriy Karchov I agree, in the property or even in the extension method: public static ComparisonResult MyCompare (this int state) {ComparisonResult result = ComparisonResult.OTHER_CASE; result = (state == -1)? ComparisonResult.IS_MINUS_ONE: result; result = (state> 0)? ComparisonResult.GREATER_THAN_ZERO: result; return result; } @avp It is possible to build everything on if, but the question was about the switch. @Spectre I think this is a matter of personal preference :) The main thing is that the approach should be uniform :) - barber

I read somewhere that switch, when the number of cases exceeds 7, is compiled into a dictionary that matches the values ​​of the checked expression to a reference to a place in the executable code. Therefore, in theory, this operator is more efficient than a set of if, if there are many options for the condition, but these options should be constants, since they should all be known at compile time.

If you need to combine switch with other conditions, you can do so

 switch (someExpression) { case val1: Do1(); break; case val2: Do2(); break; ... default: if (someExpression2(someExpression)) { Do3(); } break; } 
  • 2
    a good idea for dictionaries: <pre> Dictionary <int, Action> dict = new Dictionary <int, Action <int >> (); ... dict. Add (-1, Do1 (1)); dict.Add (0, Do2 ()); ... dict [State] (); // The necessary Action will be called ... </ pre> - Specter
  • The delegate is slower than a reference to a place in the code (obviously, C # cannot be done, even the goto operator does not allow the label to be used as a variable). Better base class with virtual method. This is faster than the delegate and the interface. - Modus