Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case in C# - a constant value is expected

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!

like image 721
johnnie Avatar asked Sep 29 '11 06:09

johnnie


People also ask

What is a switch case in C?

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.

Why is switch case used in C?

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.

What is meant by switch case?

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.


1 Answers

See C# switch statement limitations - why?

Basically Switches cannot have evaluated statements in the case statement. They must be statically evaluated.

like image 108
deepee1 Avatar answered Oct 05 '22 06:10

deepee1