Skip to content

Positional syntax for property definition – generated ctor does not use explicitly declared property automatically #27154

@deep-outcome

Description

@deep-outcome

If the generated auto-implemented property definition isn't what you want, you can define your own property of the same name. If you do that, the generated constructor and deconstructor will use your property definition.

No, it will not be used by generated .ctor.

> record Rec(int Int, string String, double Double)
. {
.     public int Int { get; } = int.Parse(String);
.     public string String { get; } = Int.ToString();
.     public double Double { get; }
. }
> new Rec(1, "3", 4.4).ToString()
"Rec { Int = 3, String = 1, Double = 0 }"

It also gets some kind of formula few words later.

You must add the explicit assignment of the declared property to its corresponding positional parameter.

This sentence is also of course wrong since source-destination dependency is exactly opposite.

This whole paragraph deservers rewording. For instance

For instance, the following example declares the FirstName and LastName properties of a positional record public, but restricts the Id positional parameter to internal.

becomes little esoteric.


Example bellow is also little beside point when showing some output and Main not relevant to explicit property declarations.

Part with deconstructor is correct.

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions