Skip to content
Learn Netverks

Lesson

Step 17/36 47% through track

records-structs

Records and structs

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Records and structs: the syntax, patterns, and safety habits you need before advancing in C#.

Teams still ship Records and structs in C# codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Records and structs in contexts like: .NET services, Unity games, and Windows-centric tooling.

Write C# with Console.WriteLine (top-level or Program), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Records are reference types optimized for immutable data with value-based equality—concise DTOs without boilerplate. Structs are value types: copied on assignment, stack-friendly for small data—unlike reference classes and unlike manual stack discipline in C++.

Record syntax

record Point(int X, int Y);

var a = new Point(1, 2);
var b = a with { X = 3 };  // non-destructive copy

Records synthesize equality, ToString, and deconstruction. Use record struct for lightweight value-type records.

Struct basics

readonly struct Color(byte R, byte G, byte B);

Avoid large structs—copying cost grows with size. Default struct constructors zero-initialize fields.

Important interview questions and answers

  1. Q: Record vs class?
    A: Records emphasize immutable data and value equality; classes suit mutable entities with identity.
  2. Q: When use struct?
    A: Small immutable value bundles (coordinates, keys)—not for polymorphic hierarchies or large payloads.

Self-check

  1. What does the with expression do on records?
  2. Are structs reference types?

Tip: Use record for immutable data shapes; use struct for small value types—avoid large structs that copy on every assignment.

Interview prep

record vs class?

Records provide value-based equality and concise syntax for immutable data; classes use reference equality by default unless overridden.

When use struct?

Small immutable value types (coordinates, IDs)—avoid large structs that copy on every assignment.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Record vs class?
  • Struct copy cost?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump