A property or indexer may not be passed as an out or ref parameter
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C Sharp, you cannot pass a property or indexer directly as an out or ref argument. This compiler error exists because properties are methods under the hood, not stable variables with guaranteed storage references. The fix is to use a local variable, then assign back to the property.
Why Properties Cannot Be ref or out
A ref or out parameter needs a true variable location that can be written to directly. A property access like obj.Count compiles into getter and setter method calls, so it does not provide a direct reference location in the same way a local variable does.
Example that fails:
The compiler blocks this pattern to avoid ambiguous or unsafe semantics.
Correct Pattern: Local Variable Then Assignment
Use a temporary variable with the out call, then copy value to the target property.
This pattern is explicit and easy to reason about in code reviews.
ref Parameters Follow the Same Rule
The same restriction applies to methods expecting ref arguments.
Again, properties are not valid ref storage targets.
Indexers Are Also Rejected
Indexers look like array positions but are also property methods. They cannot be passed as ref or out directly.
Use local copy, call method, then assign back.
Safer API Design Alternative
If you control API design, consider returning values instead of out or ref in many cases. Modern C Sharp patterns often use tuple returns or result objects.
This can reduce mutable-reference complexity and improve readability.
Performance and Maintainability Notes
The local-variable workaround is usually negligible in performance impact and clearer than trying to force ref semantics onto properties. In hot code paths, prioritize clarity first, then benchmark before optimizing.
For teams, codify this in style guidelines so junior developers recognize pattern quickly.
Testing the Assignment Path
Unit tests should verify both parse success and parse failure branches.
These tests prevent regressions when parsing logic is refactored.
Alternative API Styles That Avoid out
Modern C Sharp code often favors methods that return values directly instead of mutating out variables. For parsing-heavy workflows, returning nullable values or result objects can reduce reference-passing complexity and make pipelines easier to compose.
For example, parse utility methods can return int? and let caller assign property only when value exists. This pattern is often clearer in async and LINQ-heavy codebases.
Common Pitfalls
- Passing a property directly to
outand expecting compiler to allow it. - Forgetting to assign parsed local value back to property.
- Applying same invalid pattern to indexers and dictionary entries.
- Overusing
refAPIs when a return value would be clearer. - Ignoring failure branch and leaving old property values unintentionally.
Summary
- Properties and indexers cannot be used as direct
outorreftargets. - Use a local variable with
outorrefmethods, then assign back. - Consider API patterns that return values instead of mutable references.
- Keep parse and assignment branches explicit for maintainability.
- Add tests for both success and failure behavior.

