Skip to content
Learn Netverks

Lesson

Step 13/36 36% through track

properties-fields

Properties and fields

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

This lesson

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

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

You will apply Properties and fields 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.

A constructor initializes new objects. C# also uses propertiesget/set accessors—instead of verbose JavaBean getters for most types. Primary constructors (C# 12) shorten common patterns further.

Constructor and property example

class Product {
    public string Sku { get; }
    public double Price { get; private set; }

    public Product(string sku, double price) {
        Sku = sku;
        Price = price;
    }
}

this refers to the current instance. Auto-properties compile to backing fields—cleaner than public fields.

Init-only and required members

public required string Name { get; init; }

init allows setting during object construction but not afterward—useful for immutable DTO shapes without full records.

Important interview questions and answers

  1. Q: Property vs field?
    A: Properties expose controlled access with logic in getters/setters; public fields skip validation and break encapsulation.
  2. Q: What happens when you define any constructor?
    A: The compiler no longer generates a parameterless constructor unless you add one explicitly.

Self-check

  1. What keywords define a read-only property set at construction?
  2. Why prefer properties over public fields?

Tip: Prefer auto-properties over public fields—add validation in setters when invariants matter.

Interview prep

Property vs field?

Properties expose controlled access with getters/setters; public fields skip validation and break encapsulation.

What happens when you define any constructor?

The compiler no longer generates a parameterless constructor unless you add one explicitly.

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

  • Auto-property?
  • init-only set?

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