Skip to content

Declare readonly members for mutable structs – props with init-only setter getter also get IsReadOnlyAttribute #27201

@deep-outcome

Description

@deep-outcome

If you use auto-implemented properties, the compiler adds the readonly modifier to the get accessor for read-write properties. The compiler adds the readonly modifier to the auto-implemented property declarations for properties with only a get accessor.

Properties with init-only setter are considered readonly/immutable. If they are autoimplemented, compiler generates readonly modifier.

 object Obj { get; init; }

.method /* 060000D7 */ private hidebysig specialname instance object get_Obj () cil managed 
{
	.custom instance void [System.Runtime]System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 )
	.custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 )
	// Method begins at RVA 0x35a0
	// Code size 7 (0x7)
	.maxstack 8

System.Runtime.CompilerServices.IsReadOnlyAttribute expresses readonly modifier.

Usually by readonly property is meant property without any setter. So it is better to explicitly denote that autogenerated RW-, readonly- and with init-only-setter- properties get readonly modifier to getter.


Document Details

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


Associated WorkItem - 54742

Metadata

Metadata

Assignees

Labels

📌 seQUESTeredIdentifies that an issue has been imported into Quest.advanced-concepts/subsvcdotnet-csharp/svcokr-qualityContent-quality KR: Concerns article defects (bugs), freshness, or build warnings.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions