结构中的无参数构造函数 [英] Parameterless constructor in Structure

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

问题描述

这个问题之后在Visual Studio 2015,我正在运行 Visual Basic 14 中的新语言功能,如这里这里.

Following on from this question on using different Visual Basic versions in Visual Studio 2015, I'm running through the new lanugage features in Visual Basic 14, as documented here and here.

其中之一是能够在结构中使用无参数构造函数,如下所示:

One of those is the ability to have parameterless constructors in structures, like this:

Structure MyStruct1
   Public f As Integer
   Sub New()
      f = 15
   End Sub
End Structure

当我在 Visual Studio 2015 的代码中尝试这个时,我仍然在 New() 下看到一个红色的错误曲线:

When I try this in code in Visual Studio 2015, I'm still getting an red error squiggle under the New():

BC30629 结构不能声明没有参数的非共享Sub New".

BC30629 Structures cannot declare a non-shared 'Sub New' with no parameters.

我没有看到任何地方说这在发布之前就被撤消了.

I haven't seen anywhere that states that this got pulled before release.

我是否误解了这个新功能的作用?

Am I mis-understanding what this new feature does?

推荐答案

正如您在下面引用的文本中所见,Roslyn 删除了对具有无参数构造函数的结构的支持,因此它没有列为 VB 14 中的新功能.

As you can see in the quoted text below, Roslyn removed support for structures with parameterless constructors, and therefore it's not listed as a new feature in VB 14.

长期以来一直要求 C# 和 VB 结构构造函数总是有参数的.我们试图放松这一点C#6.0 中的要求,使结构与类更加一致.

It has been a long standing requirement that C# and VB struct constructors would always have parameters. We have tried to relax this requirements in C#6.0 to make structs more consistent with classes.

虽然结构体中的整体无参数构造函数在 IL 中是有效的透视,没有方便的方法来声明它们是几乎不存在.随着我们进行越来越多的测试,我们不断发现无参数结构构造函数导致的情况库中甚至某些版本的 CLR 中的行为不一致.

While overall parameterless constructors in structs are valid from IL perspective, without a convenient way to declare them they were virtually nonexistent. 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.

一个很好的例子是 CodePlex 错误中报告的问题 http://roslyn.codeplex.com/workitem/465.这个问题基本上是一个Activator.CreateInstance 中引入的优化围绕 CLR 4.0 和从那以后一直存在.优化假设无参数如果 T 是泛型 T 类型的实例化不会引起副作用发现是一个结构体,因此可以缓存实例.无参数结构构造函数会违反这些假设,并且使优化可观察,从而需要对现有的服务回退几个版本.

A good example is the issue reported in the CodePlex bug http://roslyn.codeplex.com/workitem/465. The issue is basically an optimization introduced in Activator.CreateInstance around CLR 4.0 and present ever since. The optimization assumes that parameterless instantiation of generic T type does not cause sideeffects if T is found to be a struct and therefore instances can be cached. Parameterless struct constructors would violate such assumptions and make optimization observable, thus necessitating servicing of existing code several versions back.

在重新考虑因做多而产生的潜在问题后长期假设,我们决定最好让我们的用户恢复对结构构造函数的要求总是具有正式的参数.


来源:恢复结构构造函数始终具有形式参数的要求.#1029

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.


Source: Restore requirement for struct constructors to always have formal parameters. #1029

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

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