Why should parameterless constructors be published in C # 6?

Parameterless constructor constructors, which until now have been banned in C #, are now implemented in Visual Studio 14 CTP (CTP 4 at the time of writing) as an experimental feature.

However, such parameterless constructors must be publicly available. You cannot make them internal or use any other access modifier.

In C # Design Notes for Aug 27, 2014 I found an explanation for this:

C #, VB and F # will all call the available parameterless constructor if they find it. If it is, but not available, C # and VB will fill in instead default(T)

. (F # will complain.)

It is problematic to have successful but different behavior of the new S () depending on where you are in the code. To minimize this problem, we need to make explicit, parameterless constructors public. So, if you want to replace the default behavior, you do it everywhere.

What is meant by "depending on where you are in the code" and how to solve the problem without using public

parameterless constructors ?

+3


source to share


2 answers


Where you are in the code

means that you will get different kinds of behavior depending on the location of the code using the framework.

For example, if you have a parameterless constructor that is marked internal, the behavior changes if you instantiate from the same assembly in which the structure is defined or from a different assembly.

In the first case, the parameterless constructor is called because its available and in the second case it is called by default (T). The same situation occurs for constructors private

and protected

.



For example, suppose we have two assemblies, A and B:

//Assembly A
public struct SomeStruct
{
   public int x = 0;
   internal SomeStruct()
   { 
       x = 10;
   }
}

public static void DoSomething()
{
      var someStruct = new SomeStruct();
      Console.WriteLine(someStruct.x); // prints 10  
}


//Assembly B
public static void DoAnotherThing()
{
    var someStruct = new SomeStruct();
    Console.WriteLine(someStruct.x); // prints 0
}

      

This leads to inconsistent behavior depending on Where you are in the code

and the reason they are forcing constructors public

.

+3


source


You can be "in place in the code" with the following meaning:

  • in the context of your structure, that is:



struct X
{
    //...

    public X CreateNew() { return new X(); }
}

      

  • in the context of your assembly:



new X();  // from the same assembly

      

  • in another assembly



new X(); // from another assembly

      

So the mechanism you are talking about would have to behave differently for the same piece of code ( new X();

) in each of these contexts. In the first example, you can use the constructor regardless of its access modifier. In the second example, you can access it if it is public

or internal

. In the third case, it could only be used if it was public

.

Doing so public

simplifies the situation as it will be available in all of these contexts.

+3


source







All Articles