Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

methods-classes

Methods and classes

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Methods and classes: the syntax, APIs, and habits you need before advancing in ASP.NET.

Teams ship Methods and classes on every ASP.NET codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Methods and classes in contexts like: Line-of-business APIs, intranets, BFF layers, and cloud-hosted services on Linux or Windows.

Write C# (top-level or Program class), 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.

ASP.NET apps are organized into classes: controllers, services, repositories, and view models. Methods encapsulate behavior; properties expose state with optional validation.

Controller-shaped class (conceptual)

public class ProductsController {
    private readonly IProductService _products;
    public ProductsController(IProductService products) {
        _products = products;
    }
    public async Task<IActionResult> Index() {
        var list = await _products.GetAllAsync();
        return Ok(list);
    }
}

Constructor injection is the default pattern—framework resolves dependencies when creating the controller.

Properties vs fields

Prefer auto-properties for DTOs: public string Name { get; set; } = ""; Records (public record UserDto(int Id, string Name);) suit immutable API responses.

Important interview questions and answers

  1. Q: Why constructor injection?
    A: Dependencies are explicit, required, and easy to mock in unit tests.
  2. Q: record vs class for DTOs?
    A: Records give value-based equality and concise syntax for read-only data transfer objects.

Self-check

  1. Where does ASP.NET inject dependencies into a controller?
  2. When prefer a record over a mutable class?

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?
  • static service risk?

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