Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    C# Language: all courses in this track

    Guided course · C# Language

    C# Internals: ref, in, out and Byref Aliasing

    Passing by value and by reference in C#: byrefs and aliasing, ref, out and in parameters, readonly refs and defensive copies, ref returns and escape rules, and the copy-versus-alias cost trade-off, with IL inspection and a benchmark.

    Advanced10 modules · 44 lessons · 5 h 14 min · 2 free

    Module 1 · Values, Parameters and Byrefs

    4 lessons · 11 min
    1. 01A value is not a variable, and assignment vs ref assignmentFree3 min
    2. 02Parameters are passed by value, and the cost of large structsFree3 min
    3. 03Byrefs: addresses to typed storage, and where they come from (included with Full Access)3 min
    4. 04ref parameters: the source-level contract and its lowering (included with Full Access)2 min

    Module 2 · Aliasing, out and in Parameters

    4 lessons · 11 min
    1. 05Aliasing is observable, and what out adds to a byref (included with Full Access)3 min
    2. 06out as a language contract, and the Try pattern vs tuples (included with Full Access)3 min
    3. 07in parameters and compiler-generated temporaries (included with Full Access)3 min
    4. 08Explicit in and overload intent (included with Full Access)2 min

    Module 3 · Readonly Refs and Defensive Copies

    4 lessons · 19 min
    1. 09Defensive copies and how to fix them (included with Full Access)4 min
    2. 10ref readonly parameters and readonly ref returns (included with Full Access)3 min
    3. 11Ref locals: a local variable can be an alias (included with Full Access)9 min
    4. 12Readonly ref locals, and properties as values (included with Full Access)3 min

    Module 4 · Ref Returns and Escape Rules

    4 lessons · 27 min
    1. 13Ref returns: the caller receives an alias (included with Full Access)11 min
    2. 14Escaping invariants, and how dangling references are prevented (included with Full Access)10 min
    3. 15scoped, and why heap objects cannot hold byref fields (included with Full Access)4 min
    4. 16Two readonly dimensions, and a preview of Span<T> (included with Full Access)2 min

    Module 5 · Copy Cost, Alias Cost and the GC

    4 lessons · 18 min
    1. 17Mutating array elements in place, and ref iteration over Span<T> (included with Full Access)3 min
    2. 18Copy cost vs alias cost: no universal size threshold (included with Full Access)2 min
    3. 19What aliasing can cost: allocation, lifetime and write barriers (included with Full Access)3 min
    4. 20Interior byrefs, object lifetime and pinning (included with Full Access)10 min

    Module 6 · Ref Patterns and Pitfalls

    4 lessons · 35 min
    1. 21Write barriers, thread safety and expected failure with out (included with Full Access)2 min
    2. 22Patterns: ref accumulators, in for large values, ref readonly access (included with Full Access)11 min
    3. 23Classes rarely need ref, and common misuse cases (included with Full Access)12 min
    4. 24Writable ref returns break encapsulation (included with Full Access)10 min

    Module 7 · Inspecting IL and Allocations

    4 lessons · 28 min
    1. 25A traced program: memory model and step-by-step execution (included with Full Access)3 min
    2. 26Inspecting IL, JIT assembly and allocations (included with Full Access)3 min
    3. 27Coding exercise: goal and starter types (included with Full Access)9 min
    4. 28Coding exercise: requirements (included with Full Access)13 min

    Module 8 · Exercise, Benchmark and Quiz

    4 lessons · 9 min
    1. 29Coding exercise: tests, implementation and memory diagram (included with Full Access)2 min
    2. 30Benchmark: project setup and predictions (included with Full Access)2 min
    3. 31Benchmark: record results and investigate (included with Full Access)2 min
    4. 32Quiz and self-assessment (included with Full Access)3 min

    Module 9 · Reference Matrices

    2 lessons · 13 min
    1. 33Reference: parameter and return matrix, IL vocabulary and rules (included with Full Access)9 min
    2. 34Copy and allocation cheat sheet, mastery checklist and sources (included with Full Access)4 min

    Module 10 · Module Practice Tests and Final Mock

    10 lessons · 2 h 23 min
    1. 35Practice Test: Values, Parameters and Byrefs (included with Full Access)12 min
    2. 36Practice Test: Aliasing, out and in Parameters (included with Full Access)12 min
    3. 37Practice Test: Readonly Refs and Defensive Copies (included with Full Access)12 min
    4. 38Practice Test: Ref Returns and Escape Rules (included with Full Access)12 min
    5. 39Practice Test: Copy Cost, Alias Cost and the GC (included with Full Access)12 min
    6. 40Practice Test: Ref Patterns and Pitfalls (included with Full Access)12 min
    7. 41Practice Test: Inspecting IL and Allocations (included with Full Access)12 min
    8. 42Practice Test: Exercise, Benchmark and Quiz (included with Full Access)12 min
    9. 43Practice Test: Reference Matrices (included with Full Access)12 min
    10. 44Final Mock Interview: 25 Questions on C# ref, in, out and Byref Aliasing (included with Full Access)35 min