In C#, the switch statement doesn’t allow cases to span ranges of values. I don’t like the idea of using if-else loops for this purpose, so are there any other ways to check numeric ranges in C#?
In C#, the switch statement doesn’t allow cases to span ranges of values. I
Share
You can use a
HashTablerespectivelyDictionaryto create a mapping ofCondition => Action.Example:
This technique is a general alternative to
switch, especially if the actions consists only of one line (like a method call).And if you’re a fan of type aliases: