在结构为参数构造函数的C#6 [英] Parameterless constructors in structs for C# 6

查看:126
本文介绍了在结构为参数构造函数的C#6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,在结构参数构造函数现在允许。

My understanding is that Parameterless constructors in structs are now allowed.

但下面给出我在VS 2015年社区编译错误

But the following gives me a compile error in VS 2015 Community

public struct Person 
{ 
    public string Name { get; } 
    public int Age { get; } 
    public Person(string name, int age) { Name = name; Age = age; } 
    public Person() : this("Jane Doe", 37) { } 
}

错误:结构可以不包含明确的参数构造函数

Error: "Structs cannot contain explicit parameterless constructors"

任何人都知道为什么。

推荐答案

的特点是存在于C#6.0老年预览,这就是为什么一些文章谈论它。但有人则删除,所以它不存在与VS 2015年RC发布的版本

The feature was present in older previews of C# 6.0, which is why some articles talk about it. But it was then removed and so it's not present in the version distributed with VS 2015 RC.

具体而言,变化中的pull要求#1106 ,与有关的问题#1029 。引用弗拉基米尔Sadov:

Specifically, the change was reverted in pull request #1106, with more information about the rationale in issue #1029. Quoting Vladimir Sadov:

由于我们进行了更多的测试,我们不停地发现,其中参的结构构造在图书馆,甚至造成不一致的行为的情况下一些版本CLR的。

As we performed more and more testing, we kept discovering cases where parameterless struct constructors caused inconsistent behavior in libraries or even in some versions of CLR.

[...]

重新考虑从打破长期存在的假设所产生的潜在问题之后,我们决定它是最适合我们的用户恢复对结构构造函数总是有形式参数的要求。

After reconsidering the potential issues arising from breaking long standing assumptions, we decided it was best for our users to restore the requirement on struct constructors to always have formal parameters.

这篇关于在结构为参数构造函数的C#6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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