TechInterviewNotes

‹ Prep paths

C# Interview Foundations: Types, LINQ, and Concurrency

Save

A practical C# interview path covering the type system, object-oriented design, generics, collections, LINQ, resource safety, async workflows, cancellation, and .NET memory management.

Course coverage

Tracks, skills, and topics covered by this prep path.

Your progress

Start this path to keep your place across devices.

Sign in to track progress

Course assessment

Full path practice

Test what you learned across every module.

Module 1

3 lessons
  1. 1
  2. 2
  3. 3
    Optional

Module 2

2 lessons
  1. 4
    Optional
  2. 5
    Optional

Module 3

3 lessons
  1. 6
    Optional
  2. 7
    Optional
  3. 8
    Optional

Module 4

1 lesson
  1. 9
    Optional

Lesson 1 of 9 | C# Type System and OOP

Value Types vs Reference Types: Copying, Boxing, and Equality

Learning goals

By the end of this lesson you should be able to explain copying semantics, identify boxing, and choose the right equality contract without repeating the misleading rule that every value type lives on the stack.

The useful mental model

A value-type variable contains its value directly. Assigning it normally copies that value. A reference-type variable contains a reference to an object; assigning it copies the reference, so both variables can observe the same object.

int first = 10;
int second = first;
second++;
// first is still 10

var alice = new Candidate { Name = "Alice" };
var alias = alice;
alias.Name = "Alicia";
// alice.Name is now "Alicia"

Storage location depends on context. A value-type field can live inside a heap object, and a local may be optimized into a register. Focus on semantics, not a stack-versus-heap slogan.

Boxing and unboxing

Boxing converts a value type to object or an implemented interface and requires an object allocation. Unboxing extracts the original value type and must use the compatible runtime type.

int score = 42;
object boxed = score;     // boxing allocation
int copy = (int)boxed;   // unboxing

Generic collections avoid repeated boxing for value types. Prefer List<int> over non-generic collections when values remain integers.

Equality choices

  • Value types receive field-based ValueType.Equals behavior unless overridden.
  • Classes use reference equality by default.
  • Records provide value-oriented equality that is useful for immutable data models.
  • Implement IEquatable<T> when a domain type needs an explicit, efficient equality contract.

Interview checkpoints

  1. Assignment copies a value for structs and a reference for classes.
  2. Boxing is a conversion plus allocation, not merely a cast.
  3. Mutable structs are risky because hidden copies make updates surprising.
  4. Equality and identity are separate design decisions.
A strong answer describes observable semantics first and treats storage as an implementation detail shaped by context and runtime optimization.
C# Interview Foundations: Types, LINQ, and Concurrency — TechInterviewNotes