Skip to content

[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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

[WIP] Why CS0236 #47119

wants to merge 3 commits into from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 7, 2025

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:

[Enter feedback here]
I mean Why Compiler will report this error . What happened in Compiler when this error occurs?


Document Details

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

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

📄 File 🔗 Preview link
docs/csharp/misc/cs0236.md docs/csharp/misc/cs0236

@BillWagner
Copy link
Member

@copilot Can you start working on this again?

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 8, 2025

@copilot Can you start working on this again?

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.

@dotnet-policy-service dotnet-policy-service bot added okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. dotnet-csharp/svc csharp-errors-warnings/subsvc labels Jul 8, 2025
Copilot finished work on behalf of BillWagner July 8, 2025 14:03
Copy link
Member

@BillWagner BillWagner left a 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.


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.
Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -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
Copy link
Member

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.

Copy link
Contributor Author

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.

@BillWagner
Copy link
Member

@copilot Please continue working on this issue

@BillWagner
Copy link
Member

@copilot Please start working on this again

@BillWagner
Copy link
Member

@copilot Can you start working on this again?

@BillWagner
Copy link
Member

@copilot Can you start working on this again?

…r and rule purpose

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 10, 2025

This is a great start, but there are some errors and corrections to make.

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.

@copilot Can you start working on this again?

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.

Copilot finished work on behalf of BillWagner July 10, 2025 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
csharp-errors-warnings/subsvc dotnet-csharp/svc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why CS0236
2 participants