Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primary constructors in C# vnext

Tags:

c#

The Language feature implementation status was brought to my attention via C# Chat. I have a couple of questions about some of the features not covered in this existing question.

Some of the new features are obvious such as Getter-only auto-properties

public int Y { get; } = y;

... awesome.

But others I am not clear on...


Primary constructors

class Point(int x, int y) { … }

It may be a gap in my knowledge, but I haven't come across the term "primary constructor" before, but some research seems to suggest that basically this is a shortcut for defining the following

class Point
{
    private int x;
    private int y;

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
  • Is this the correct interpretation?
  • Does this mean you can still do constructor overloads (the term "primary" implies so)?
  • Can you refer to these implicit backing fields in other constructors/methods?
like image 358
dav_i Avatar asked Apr 07 '14 09:04

dav_i


People also ask

What is a primary constructor?

Constructors The primary constructor is a part of the class header, and it goes after the class name and optional type parameters. class Person constructor(firstName: String) { /*...*/ }

What is primary constructor in C#?

Basically a Primary Constructor is a feature of C# that was announced with Visual Studio 2014 by which a class or structure can accept parameters in the class definition without a formal constructor declaration.

What are constructors in C?

A Constructor in C is used in the memory management of C++programming. It allows built-in data types like int, float and user-defined data types such as class. Constructor in Object-oriented programming initializes the variable of a user-defined data type. Constructor helps in the creation of an object.

What is a constructor in C++?

A constructor is a special method of the class which gets automatically invoked whenever an instance of the class is created. Like methods, a constructor also contains the collection of instructions that are executed at the time of Object creation.

Do primary constructors work with classes and structs?

At the time of this writing, primary constructors only seem to work with records. Classes and structs are not supported yet but should be implemented in the final release. Did you notice the subtle difference?

What is the default constructor of a class?

The default constructor initializes all numeric fields to zero and all string and object fields to null inside a class. Note : This will also show some warnings as follows:

Can constructor have return type in C++?

Constructors don’t have return type; A constructor is automatically called when an object is created. It must be placed in public section of class. If we do not specify a constructor, C++ compiler generates a default constructor for object (expects no parameters and has an empty body).


1 Answers

  • Is this the correct interpretation?

Almost, but not exactly. By default, primary constructor parameters are not captured into fields; they are, however, accessible in instance member initializers (and only there), so you can explicitly initialize fields or properties with them:

class Point(int x, int y)
{
    public int X { get; } = x;
    public int Y { get; } = y;
}

(note that in this case, backing fields are created, but for the properties, not the constructor parameters)

But you can also declare the parameters with access modifiers, in which case they will be captured as fields, and be accessible from all instance members:

class Point(public readonly int x, public readonly int y)
{
}

(as you can see, you can also specify other modifiers for the fields, like readonly)

  • Does this mean you can still do constructor overloads (the term "primary" implies so)?

Yes, you can have other, non-primary constructors. However, they're all required to call the primary constructor, with the usual : this(...) syntax.

  • Can you refer to these implicit backing fields in other constructors/methods?

As mentioned above, there are no implicit backing fields; the backing field is only created if you explicitly specify an access modifier on the primary constructor parameter.


EDIT: this feature has been withdrawn and won't be in C# 6 (probably in C# 7, in a slightly different form)

like image 150
Thomas Levesque Avatar answered Oct 17 '22 20:10

Thomas Levesque