Question

I've got a case statement in c#. I would like to chose the values for the cases from a configuration file at run time. Is this possible?

Was it helpful?

Solution

Not with a switch statement, no. The case labels have to be compile-time constants.

Marc Gravell has a switch-like construct you could use, somewhere... I'll try to find it. It may well not be suitable for your particular usage though.

Otherwise, a sequence of if/else if/else if [...] /else is the way to go.

OTHER TIPS

As the values being used in a case statement in C# are expected to be constants I don't think it is possible to set these at runtime from a config file.

As others have said, the switch statement needs the values at compile time since the underlying hash table is built at compile time. If you have entries that are determined at runtime, I would use hash tables / dictionaries with command pattern or delegates if I were you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top