Switch case in C# - a constant value is expected(C# 中的 switch case - 需要一个常量值)
问题描述
我的代码如下:
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");
}
}
但这不起作用.case 语句给了我一个错误,说需要一个常量变量.请大家帮忙谢谢!
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!
推荐答案
查看C# switch 语句限制 - 为什么?
基本上,开关不能在 case 语句中包含评估语句.必须对它们进行静态评估.
Basically Switches cannot have evaluated statements in the case statement. They must be statically evaluated.
这篇关于C# 中的 switch case - 需要一个常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!