试图复制“必须声明身体”编译器错误 [英] Trying to reproduce "must declare a body" compiler error

查看:462
本文介绍了试图复制“必须声明身体”编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重现



为什么编译器不会抛出任何错误?

解决方案

您正在使用Visual Studio 2015,你的目标.NET 4是不相关的 - 大多数C#6语言功能不依赖于框架的功能。您所使用的C#6代码可以轻松地编译而不需要参考任何现代CLR或框架特性 - 如果语言设计人员决定使用.NET 1.0,则可以使用它。)



您需要将语言级别设置为C#5才能在此处看到错误。在项目属性/构建/高级对话框中执行此操作:





您会收到以下错误:


错误CS8026:C#5中不提供特性'readonly自动实现的属性' 。请使用语言版本6或更高版本。


诚然,这不是你真正想看到的错误 - 我想你需要请使用早期版本的编译器来获取确切的错误。


I'm trying to reproduce the C# compiler error CS0840 with the exact code that's given in the website:

class Test36
{
    public int myProp { get; } // CS0840

    // to create a read-only property
    // try the following line instead
    public int myProp2 { get; private set; }

}

public Form1()
{
    InitializeComponent();

    Test36 test = new Test36();
}

I'm running it on .NET 4.0 using Visual Studio Community 2015. Surprisingly, I cannot reproduce it. Compiler doesn't throw any error:

Why the compiler isn't throwing any error?

解决方案

You're using Visual Studio 2015, which implements C# 6. The fact that you're targeting .NET 4 is irrelevant - most of the C# 6 language features don't depend on framework features at all. The C# 6 code you're using can easily be compiled without reference to any modern CLR or framework features - it could have worked with .NET 1.0 if the language designers had decided to :)

You'll need to set you language level to C# 5 to see an error here. Do that in the project properties / Build / Advanced dialog:

You'll then get this error:

error CS8026: Feature 'readonly automatically implemented properties' is not available in C# 5. Please use language version 6 or greater.

Admittedly that's not the error you actually wanted to see - I think you'll need to use an earlier version of the compiler to get that exact error.

这篇关于试图复制“必须声明身体”编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆