Skip to content
Learn Netverks

Lesson

Step 16/36 44% through track

interfaces-csharp

Interfaces

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

This lesson

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

Interfaces and DI underpin ASP.NET Core—this language track sets up the web track.

You will apply Interfaces 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.

Interfaces define contracts without implementation—like Java interfaces but C# allows explicit interface implementation and default interface methods (C# 8+). Use interfaces when multiple unrelated types share a capability.

Interface example

interface IShape {
    double Area();
}

class Circle : IShape {
    public double Radius { get; }
    public Circle(double r) => Radius = r;
    public double Area() => Math.PI * Radius * Radius;
}

Program to interfaces (IShape) so callers stay decoupled from concrete types—essential before DI containers in ASP.NET.

Important interview questions and answers

  1. Q: Abstract class vs interface?
    A: Abstract classes can hold state and shared implementation; interfaces are pure contracts (with optional defaults)—prefer interfaces for capability mixing.
  2. Q: Why virtual on base methods?
    A: Without virtual, overrides are hidden, not polymorphic—calls through base references won't dispatch to derived code.

Self-check

  1. What makes polymorphism work through a base reference?
  2. Can a struct implement an interface?

Tip: A class can implement many interfaces—prefer small, focused interfaces over one giant contract.

Interview prep

Interface vs abstract class?

Interfaces define contracts without state; abstract classes can share implementation and fields—a class inherits one base but many interfaces.

Explicit interface implementation?

Implement an interface member with the interface prefix—callable only through the interface type, not the class directly.

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

  • Default interface method?
  • IEnumerable role?

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