-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
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.
- ID: ac8804c7-6acd-ab2a-4e14-0b99265969a5
- Version Independent ID: 183efcbd-a781-d303-f9cd-1c22da1676de
- Content: Write safe and efficient C# code
- Content Source: docs/csharp/write-safe-efficient-code.md
- Product: dotnet-csharp
- Technology: csharp-advanced-concepts
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn