Replies: 2 comments 1 reply
-
Why is this the expected behavior? I'd say the closest equivalent code for a class is: public class Example
{
public Example()
{
A = 0;
B = 0;
}
public Example(int a) : this()
{ }
public int A { get; }
public int B { get; } = 10;
} This code executes in the opposite order: |
Beta Was this translation helpful? Give feedback.
1 reply
-
@pkulikov - At the company I work for, In order to make intent clear, we don't allow a property/field to be initialized from a constructor and a default value. Initializing it through multiple methods makes understanding intent confusing as you see here. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First of all, the C# 9-compliant code that doesn't compile (CS0843: Auto-implemented property 'Program.Example.A' must be fully assigned before control is returned to the caller):
To fix the build error, let's zero all the fields with the
this()
call:However, when, in C# 10, I add the field initializer, the code produces the same error as the first example (CS0843: Auto-implemented property 'Program.Example.A' must be fully assigned before control is returned to the caller):
That doesn't look consistent: if the second example is fine, why the third one fails to compile? Is
: this()
part ignored by the compiler? I would expect all fields to be zeroed and thenB
set to10
.Beta Was this translation helpful? Give feedback.
All reactions