相关疑难解决方法(0)

在C#中用类型推断一个常量

在C#中,以下类型推断有效:

var s = "abcd";
Run Code Online (Sandbox Code Playgroud)

但是,当变量是常数时,为什么不能推断出类型呢?

以下引发了编译时异常:

const var s = "abcd"; // <= Compile time error: 
                      //    Implicitly-typed local variables cannot be constant
Run Code Online (Sandbox Code Playgroud)

c# type-inference constants

40
推荐指数
4
解决办法
3940
查看次数

标签 统计

c# ×1

constants ×1

type-inference ×1