c# - 常量修改编译报错

标签 c# visual-studio compiler-errors constants

在 Visual Studio 2015 中运行以下代码

class Program
{
    static void Main(string[] args)
    {
        const int a = 0;
        a = 5;
    }
}

显然 会生成编译器错误,但错误是:

The left-hand side of an assignment must be a variable, property or indexer

我接受代码应该生成编译器错误,但这个措辞对我来说没有多大意义,因为赋值的左侧 (a) 是一个变量 - an整数变量(恰好是常量)

为什么我的 a 变量没有被识别为变量?

最佳答案

该错误消息是正确的。

MSDN说:

Constant fields and locals aren't variables

所以常量不是变量、属性或索引器之一。

关于c# - 常量修改编译报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39399978/

相关文章:

c++ - 与空项目相比,在 Visual Studio 中使用模板创建解决方案实际上有何作用?

c++ - 漂亮的打印嵌套 vector 图

c# - 如何将xml文件绑定(bind)到Gridview

c# - LinqToSql 查询中的条件快捷方式

c# - 如何调试 Fiddler 扩展

vba - 为什么可以使用附加参数调用返回Variant的函数?

ios - iOS : Use of undeclared identifier error in simple calculator [closed]

c# - Caliburn Micro Xamarin 的数据绑定(bind)操作顺序

c# - 查找 Button/UIElement 在屏幕上相对于网格 Windows Phone 的位置

c++ - 线程调用成员函数