C# 中的 switch 语句和“需要一个常量值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/466565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:31:43  来源:igfitidea点击:

switch statement in C# and "a constant value is expected"

c#visual-studio

提问by w4ik

Why does the compiler say "a constant value is required" for the first case...the second case works fine...

为什么编译器在第一种情况下说“需要一个常量值”……第二种情况工作正常……

switch (definingGroup)
{
    case Properties.Settings.Default.OU_HomeOffice:  
        //do something  
        break;
    case "OU=Home Office":  
        //do something
        break;
    default:
        break;
 }

also tried...

也试过...

switch (definingGroup)
{
    case Properties.Settings.Default.OU_HomeOffice.ToString():  
        //do something
        break;
    case "OU=Home Office":
        //do something
        break;
    default:
        break;
 }

...same error

...同样的错误

Here's the Properties.Setting code

这是 Properties.Setting 代码

[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("OU=Home Office")]
public string OU_HomeOffice {
    get {
        return ((string)(this["OU_HomeOffice"]));
    }
}

采纳答案by Jon Skeet

Properties.Settings.Default.OU_HomeOfficeisn't a constant string - something known at compile time. The C# switch statement requires that every case is a compile-time constant.

Properties.Settings.Default.OU_HomeOffice不是常量字符串 - 在编译时已知的东西。C# switch 语句要求每个 case 都是编译时常量。

(Apart from anything else, that's the only way it can know that there won't be any duplicates.)

(除此之外,这是它知道不会有任何重复项的唯一方法。)

See section 8.7.2 of the C# 3.0 spec for more details.

有关更多详细信息,请参阅 C# 3.0 规范的第 8.7.2 节。

回答by Jim Petkus

This is because the value cannot be determined at compile time (as it is coming out of a configuration setting). You need to supply values that are known at the time the code is compiled (constants).

这是因为在编译时无法确定该值(因为它来自配置设置)。您需要提供编译代码时已知的值(常量)。

回答by Jesse Taber

What it's basically saying is that it needs to ensure that the value for each case will not change at runtime. Hard-coding your string in-line like you did on the second case will ensure that the value won't change at run time (as would declaring a 'const' variable and assigning it the hard-coded string as a value).

它的基本意思是它需要确保每个案例的值在运行时不会改变。像在第二种情况下那样对字符串进行内嵌硬编码将确保该值不会在运行时更改(就像声明“const”变量并将硬编码字符串分配为值一样)。

The first case is making a call into a property of a class, the value of which isn't known to the compiler at compile time.

第一种情况是调用类的属性,编译器在编译时不知道该属性的值。

If you have some 'configuration' values that are pretty much doing to be constant within your application, you might consider creating a class where you can hard-code these values are const variables and use those in your switch statements. Otherwise, you're likely going to be stuck with having to use if/else if statements.

如果您有一些“配置”值在您的应用程序中几乎保持不变,您可以考虑创建一个类,您可以在其中将这些值硬编码为 const 变量并在您的 switch 语句中使用这些值。否则,您可能会不得不使用 if/else if 语句。