Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

classes-objects

Classes and objects

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

This lesson

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

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

You will apply Classes and objects in contexts like: Domain models, DTOs, and service abstractions before ASP.NET wiring.

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 class is a blueprint; an object is a runtime instance created with new. If you know Java classes, C# feels familiar—everything lives in types—but C# adds properties, value types, and reference semantics on the CLR heap.

Defining and using a class

class User {
    public string Name = "";
    public void Greet() {
        Console.WriteLine($"Hi, {Name}");
    }
}

var u = new User { Name = "Ada" };
u.Greet();

Object initializer syntax sets fields or properties at construction time. All objects are reference types unless declared as structs.

Reference semantics

User a = new User(); User b = a; — both references point to the same object; mutating through b affects a—same model as Java, unlike value-copy structs in C++ stack objects.

Important interview questions and answers

  1. Q: Class vs object?
    A: Class is the type definition; object is a heap instance with its own field values.
  2. Q: new keyword?
    A: Allocates memory on the managed heap and runs the constructor; GC reclaims when unreachable.

Self-check

  1. What operator creates an object?
  2. How does C# OOP differ from JavaScript prototypes?

Tip: Class instances are reference types on the managed heap—assigning b = a copies the reference, not the object, like Java.

Interview prep

What does new do?

Allocates a reference-type instance on the managed heap and runs the constructor.

Every class inherits from?

object—all types get ToString, Equals, and GetHashCode.

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

  • Class vs record?
  • this keyword?

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