-
Notifications
You must be signed in to change notification settings - Fork 6k
[WIP] Why CS0236 #47119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[WIP] Why CS0236 #47119
Conversation
@copilot Can you start working on this again? |
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
I've enhanced the CS0236 documentation to address the original issue. The documentation now explains why the compiler reports this error, including the underlying reasons about field initialization order and object construction phases. See commit 0c88707. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great start, but there are some errors and corrections to make.
docs/csharp/misc/cs0236.md
Outdated
|
||
The compiler enforces this restriction because of how object initialization works in C#. When an object is created, field initializers are processed before any constructor code runs. During this phase: | ||
|
||
1. **Initialization order is not guaranteed**: The C# specification doesn't define the order in which field initializers are executed. If one field could reference another, the referenced field might not be initialized yet. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is false. Field initializers are processed in lexical order in a single file. The only non-guarantee is for partial
class and struct types, where the order of the fields initializers in different source files isn't specified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the incorrect statement about initialization order. The documentation now correctly states that field initializers are processed in lexical order within a single file, with the caveat about partial classes across different source files. See commit 70fe1de.
docs/csharp/misc/cs0236.md
Outdated
@@ -26,13 +44,47 @@ public class MyClass | |||
{ | |||
public int i = 5; | |||
|
|||
// To fix the error, remove "= i", and uncomment the line in constructor. | |||
// CS0236: Field initializer cannot reference instance field 'i' | |||
// This fails because 'i' might not be initialized when 'j' is being initialized |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite true. The rule exists to class authors can rearrange fields without introducing compiler errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected the explanation to focus on the rule's purpose of allowing class authors to rearrange fields without introducing compiler errors. See commit 70fe1de.
@copilot Please continue working on this issue |
@copilot Please start working on this again |
@copilot Can you start working on this again? |
@copilot Can you start working on this again? |
…r and rule purpose Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Addressed the errors and corrections mentioned in the review feedback. Fixed the inaccurate statements about field initialization order and the rule's purpose. See commit 70fe1de.
I've continued working on the issue and addressed the review feedback about inaccuracies in the CS0236 documentation. The corrections have been made in commit 70fe1de. |
Thanks for assigning this issue to me. I'm starting to work on it and will keep this PR's description up to date as I form a plan and make progress.
Original issue description:
Fixes #24369.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
Internal previews