Skip to content

Fix static field documentation to clarify storage behavior for generic types #47091

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

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 3, 2025

The documentation incorrectly stated that there is "only one copy" of each static field, which is misleading for generic types. For generic types, each closed generic type (e.g., MyClass<int> vs MyClass<string>) has its own separate storage location for static fields.

Changes Made

Updated three documentation files to add clarifying language about static field storage in generic types:

  • docs/csharp/language-reference/keywords/static.md - Added clarification after the statement about "only one copy of each static field"
  • docs/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members.md - Added clarification in the "Static Members" section
  • docs/csharp/misc/cs1914.md - Added clarification in the compiler error documentation

Example

class MyClass<T>
{
    static int count;
}

// These are separate storage locations:
MyClass<int>.count = 1;
MyClass<string>.count = 2;
// MyClass<int>.count is still 1, MyClass<string>.count is 2

The changes are minimal and surgical, preserving the existing content while adding the necessary clarification. The terminology continues to use "static field" as recommended by @BillWagner, avoiding the ambiguous "class field" term.

Fixes #22805.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Jul 3, 2025
@Copilot Copilot AI changed the title [WIP] 'class field' might be more in place than 'static field' Fix static field documentation to clarify storage behavior for generic types Jul 3, 2025
Copilot finished work on behalf of BillWagner July 3, 2025 16:17
@Copilot Copilot AI requested a review from BillWagner July 3, 2025 16:17
@@ -39,7 +39,7 @@ To refer to the `static` member `x`, use the fully qualified name, `MyBaseC.MySt
Console.WriteLine(MyBaseC.MyStruct.x);
```

While an instance of a class contains a separate copy of all instance fields of the class, there's only one copy of each `static` field.
While an instance of a class contains a separate copy of all instance fields of the class, there's only one copy of each `static` field. For generic types, each closed generic type has its own copy of static fields.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who about static fields marked with [ThreadStatic]? They have multiple copies too (one per thread).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right! I've updated the documentation to also mention ThreadStatic fields. The text now clarifies that static fields marked with ThreadStaticAttribute have one copy per thread, in addition to the existing clarification about generic types. See commit 578c658.

Copilot finished work on behalf of jkotas July 3, 2025 21:19
@Copilot Copilot AI requested a review from jkotas July 3, 2025 21:19
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 fundamentals/subsvc lang-reference/subsvc 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.

'class field' might be more in place than 'static field'
3 participants