Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterless constructor in Structure

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

When I try this in code in Visual Studio 2015, I'm still getting an red error squiggle under the 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?

like image 657
Jon Egerton Avatar asked Aug 24 '15 10:08

Jon Egerton


People also ask

What is a Parameterless constructor?

A constructor that takes no parameters is called a parameterless constructor. Parameterless constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new . For more information, see Instance Constructors.

Can I write a default constructor in structure?

The simple answer is yes.

Can I write a default constructor in structure C#?

C# does not allow a struct to declare a default, no-parameters, constructor. The reason for this constraint is to do with the fact that, unlike in C++, a C# struct is associated with value-type semantic and a value-type is not required to have a constructor.

Can a struct have a constructor?

Constructor creation in structure: Structures in C cannot have a constructor inside a structure but Structures in C++ can have Constructor creation.


1 Answers

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.

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.

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.

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.

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
like image 197
Bjørn-Roger Kringsjå Avatar answered Sep 30 '22 01:09

Bjørn-Roger Kringsjå