试图重现"必须声明一个身体QUOT;编译器错误 [英] Trying to reproduce "must declare a body" compiler error
问题描述
我试图重现 C#编译器错误CS0840与该公司在网站上给出确切的代码:
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();
}
我在.NET 4.0使用Visual Studio 2015年社区运行令人惊讶的是我不能复制。编译器不会引发任何错误:
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?
推荐答案
您正在使用Visual Studio 2015年,它实现了C#6.事实上,你的目标.NET 4无关 - 大多数的C#6语言功能不依赖于框架的所有功能。您正在使用的C#代码6罐的轻松的未经被编译成任何现代CLR或框架的功能 - 它可以使用.NET 1.0工作过,如果语言设计者决定:)
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 :)
您需要将您的语言级别设置为C#5在这里看到一个错误。这样做,在项目属性/建设/高级对话框:
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:
您会再得到这个错误:
错误CS8026:功能'只读自动实现的属性是不是在C#5可用。请使用语言版本6或更高
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.
这篇关于试图重现"必须声明一个身体QUOT;编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!