My code is as follows:
public static void Output<T>(IEnumerable<T> dataSource) where T : class { dataSourceName = (typeof(T).Name); switch (dataSourceName) { case (string)typeof(CustomerDetails).Name.ToString(); : var t = 123; break; default: Console.WriteLine("Test"); } }
But this is not working. The case statement is giving me an error saying that a constant variable is expected. Please help guys thank you!
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
In C++, the switch statement is used for executing one condition from multiple conditions. It is similar to an if-else-if ladder. Switch statement consists of conditional based cases and a default case. In a switch statement, the “case value” can be of “char” and “int” type.
The switch case in java executes one statement from multiple ones. Thus, it is like an if-else-if ladder statement. It works with a lot of data types. The switch statement is used to test the equality of a variable against several values specified in the test cases.
See C# switch statement limitations - why?
Basically Switches cannot have evaluated statements in the case statement. They must be statically evaluated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With