Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
A ref local variable is a local variable that acts as a direct alias to another variable’s memory location rather than storing a discrete copy of its value. Any read or write operation performed on the ref local is executed directly against the underlying memory address of the aliased variable.
Syntax and Initialization
To declare a ref local, the ref keyword must precede both the type declaration and the initialization expression.
int originalVariable = 42;
// 'refLocal' is now an alias for 'originalVariable'
ref int refLocal = ref originalVariable;
// Mutating the ref local mutates the original memory location
refLocal = 100;
// originalVariable is now 100
Reference Reassignment (C# 7.3+)
By default, assigning a value to a ref local overwrites the value at the referenced memory location. However, using the ref keyword during assignment allows the ref local to be repointed to an entirely different memory location.
int valueA = 10;
int valueB = 20;
ref int alias = ref valueA; // Points to valueA
// Standard assignment: Mutates the value at the current memory location (valueA becomes 50)
alias = 50;
// Reference reassignment: Changes the memory location 'alias' points to
alias = ref valueB;
// Mutates valueB
alias = 99;
ref readonly Locals
You can enforce immutability on the aliased memory location by declaring the local as ref readonly. This permits reading from the memory address while preventing any modifications through the alias.
int mutableValue = 5;
ref readonly int readOnlyAlias = ref mutableValue;
// Valid: Reading the value
int copy = readOnlyAlias;
// Compiler Error: Cannot assign to a readonly ref local
// readOnlyAlias = 10;
Technical Constraints and Rules
- Mandatory Initialization: A
ref local must be initialized at the time of declaration. It cannot be declared uninitialized and assigned later.
// Compiler Error: A ref local must be initialized
// ref int invalidRef;
2. **Type Matching:** The type of the `ref` local must strictly match the type of the variable it aliases. Implicit conversions are not permitted because the alias must map exactly to the underlying memory layout.
3. **Escape Analysis (Lifetime Rules):** The C# compiler enforces strict lifetime rules (safe-to-escape rules) to prevent dangling pointers. A `ref` local cannot refer to a variable that has a narrower scope or shorter lifetime than the `ref` local itself.
4. **Property Restrictions:** A `ref` local cannot alias a property or an indexer that does not return by reference, because properties are methods under the hood and do not inherently represent a stable memory location.
<div style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
maxWidth: "754px",
padding: "1rem 0",
marginBottom: "24px"
}}>
<span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Master C# with Deep Grasping Methodology!</span>
<a
href="https://syntblaze.com"
target="_blank"
style={{
textDecoration: "none",
backgroundColor: "#007AFF",
color: "#ffffff",
padding: "6px 16px",
borderRadius: "16px",
fontSize: "0.9rem",
fontWeight: "600",
textAlign: "center",
transition: "background-color 0.2s ease"
}}
>
Learn More
</a>
</div>
<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
<img src="/images/skill-tracking.png" style={{ width: "30%", minWidth: 60 }} />
<img src="/images/nuggets.png" style={{ width: "30%", minWidth: 60 }} />
<img src="/images/bite-sized-exercises.png" style={{ width: "30%", minWidth: 60 }} />
</div>
<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
<img src="/images/mastery-chain.png" style={{ width: "30%", minWidth: 60 }} />
<img src="/images/element-previews.png" style={{ width: "30%", minWidth: 60 }} />
<img src="/images/element-explanations.png" style={{ width: "30%", minWidth: 60 }} />
</div>