Repository navigation
Define mutable, immutable, and readonly in the C# type system overview (Fixes #42584) - #56673
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The documentation overstates immutability and read-only access guarantees, omits mutable-reference caveats relevant to thread safety, and includes invalid section links.
3 open issues
What changed in this PR
Expands the C# type-system overview with definitions of mutable, immutable, and readonly, addressing the terminology gap reported in #42584.
Changes:
- Adds explanations of mutability and immutability, including their relationship to shared data and thread safety.
- Describes
readonlyusage for fields, structs, members, parameters, and returns, with links to detailed references.
| File | Description |
|---|---|
docs/csharp/fundamentals/types/index.md |
Adds a mutability and readonly section before the type-selection guidance. |
Review effort: Balanced
2c0583f to
2aa8f4b
Compare
…guarantees, and fix anchors
BillWagner
left a comment
There was a problem hiding this comment.
This needs some changes. I wrote a detailed comment on how this explanation needs to be reframed.
|
|
||
| All types ultimately derive from <xref:System.Object?displayProperty=nameWithType>. Value types derive from <xref:System.ValueType?displayProperty=nameWithType>, which derives from `object`. This unified hierarchy is called the [Common Type System](../../../standard/base-types/common-type-system.md) (CTS). For more information about inheritance, see [Inheritance](../object-oriented/inheritance.md). | ||
|
|
||
| ## Mutability, immutability, and readonly |
There was a problem hiding this comment.
This needs work. The explanations are muddled, as are the concepts used to describe them.
mutable and immutable describe how an instance can be changed, or not. readonly is a keyword that enforces immutability. Fields, Structs, Members, Parameters, and returns mix types, members, specific members, variables and expressions. As a result the reader will leave with more questions than when they started. For example, how is a struct related to a parameter? Why are they in the same list?
Consider an organization like this:
- Mutable and Immutable describe the restrictions on an instance of a type: Does it store state, and can that state be modified after initialization? There's the nuance on shallow and deep mutability to cover here.
- Next, how does the language support these concepts? There are a couple ways: "expressions" are always rvalues. "variables" are lvalues. (C# usually avoids the terms lvalue and rvalue, but the concepts map to variable and expression.). Further, a variable can be restriction by the use of the
readonlymodifier when the variable resolves to areadonlymember of a struct, or a member of areadonly struct. - Next,
structtypes are pass-by value, so mutations can occur on a copy. (withexpressions are a good example of this as well). - Finally,
recordtypes don't enforce immutability, but rather don't violate it. In other words, none of the members synthesized by the compiler mutate state. However, a developer can add members that do mutate a record instance. The language doesn't enforce immutability on records.
Does that help?
There was a problem hiding this comment.
Thank you for the clear organization suggestion! I have restructured this section in ac43c9a around instance state (mutable vs. immutable, shallow vs. deep), expressions vs. variables with readonly restrictions, struct pass-by-value/with copy semantics, and records not enforcing immutability.

Summary
Internal previews
Build report