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...
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;
}
}
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) { /*...*/ }
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.
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.
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.
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?
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:
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).
- 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With