-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
The page says:
The execution of the get accessor is equivalent to reading the value of the field. For example, when you're returning the private variable from the get accessor and optimizations are enabled, the call to the get accessor method is inlined by the compiler so there's no method-call overhead. However, a virtual get accessor method can't be inlined because the compiler doesn't know at compile-time which method may actually be called at run time.
My interpretation is this is correct but slightly confusing. Specifically, the emphasis is on "the call to", as in
- the get accessor method does not get inlined, but
- the call to it may get inlined
This is even more confusing by the "However" sentence afterwards, which is a non sequitor — the get accessor method itself doesn't get inlined regardless.
This is indeed the behavior I observe.
Can we expand this to make it clearer? For example:
For example, when you're returning the private variable from the get accessor and optimizations are enabled, the call to the get accessor method is inlined by the compiler so there's no method-call overhead. However, a get accessor does still get emitted. In addition, a call to a virtual get accessor method can't be inlined because the compiler doesn't know at compile-time which method may actually be called at run time.
[Enter feedback here]
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
- ID: 322a60f9-0bd8-b432-653a-069c477a93fe
- Version Independent ID: dbc29edd-b142-f2d7-4824-dec9951f32c7
- Content: Using Properties - C# Programming Guide
- Content Source: docs/csharp/programming-guide/classes-and-structs/using-properties.md
- Product: dotnet-csharp
- Technology: csharp-fundamentals
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn