dotnet / dotnet/csharpstandard

Inaccurate description for `Object initializers` rule

Open
#303 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

In the description of how **object initializers** work, [the language spec](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#object-initializers) states that this initialization

```
Point a = new Point { X = 0, Y = 1 };
```

is equivalent to the following statements:

```
Point __a = new Point();
__a.X = 0;
__a.Y = 1;
Point a = __a;
```

Yes, that is correct. However, in my opinion the wording used is inaccurate. The specification could better distinguish the declarator type from the initialized type, even if such differentiation is done in the wording only (if it is there, I was not able to find it). Consider

```
SomeInterface a = new Point { X = 0, Y = 1 };
```

where `Point` implements `SomeInterface`. Naively passing through the text *"A member initializer that specifies an expression after the equals sign is processed in the same way as an assignment"* can be misleading, and the translation above wouldn't apply when `X` and `Y` are defined in `Point`. In fact, I just caught an error in my code rewriter for the case in question.

Again, the specification does not seem to be wrong in the way it is right now, but I would suggest making it a bit more precise.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.