Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Programming · C# · Chapter 05 / 10

Programming learning path

Interfaces, abstract classes, structs & records

These C# type forms solve different design problems and should not be treated as synonyms.

Interface vs abstract class

An interface describes a capability or contract that unrelated types can implement. A class can implement multiple interfaces. An abstract class can combine abstract members with shared state, constructors and implemented behavior, but a class can inherit from only one base class.

public interface IHealthCheck
{
    bool IsHealthy();
}

public class ApiHealthCheck : IHealthCheck
{
    public bool IsHealthy() => true;
}

Structs and records

A struct is a value type and works best for small value-like data where copy semantics make sense. A record is designed for data-centric models and provides value-based equality by default. record is a reference type; record struct is a value type.

public readonly record struct Coordinate(int X, int Y);

var first = new Coordinate(10, 20);
var second = new Coordinate(10, 20);
Console.WriteLine(first == second); // True

Key points

  • Interface: behavioral contract that many unrelated types can implement.
  • Abstract class: shared base behavior/state plus incomplete members.
  • Struct: value type with copy semantics.
  • Record: data-oriented type with value equality semantics by default.

Source registry

3 chapter references
Interfaces

Interface contracts and implementation

Microsoft Learn · Official documentation
Source ↗
The C# type system

Value/reference types, nullability, conversions and type-system fundamentals

Microsoft Learn · Official documentation
Source ↗
Object-oriented programming

Encapsulation, inheritance, polymorphism and abstraction

Microsoft Learn · Official documentation
Source ↗