Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An interface in C# is a reference type that defines a strict contract consisting of signatures for methods, properties, events, and indexers. Any non-abstract class or struct that implements the interface must provide concrete implementations for all of its members. An abstract class can implement an interface and satisfy the contract by declaring the interface members as abstract, deferring the concrete implementation to derived classes. Interfaces cannot be instantiated directly. By convention, interface names in C# begin with a capital I.
using System;

public interface IProcessable
{
    // Property signature
    int Id { get; set; }

    // Method signature
    void Process(string data);

    // Event signature
    event EventHandler ProcessCompleted;
}

Implementation Mechanics

A class or struct declares its implementation of an interface using the : operator. The implementing type must fulfill the entire contract defined by the interface.
using System;

public class DataProcessor : IProcessable
{
    // Implicit implementation requires public access modifiers
    public int Id { get; set; }

    public void Process(string data)
    {
        // Concrete implementation logic
    }

    public event EventHandler ProcessCompleted;
}

Technical Characteristics and Rules

  • Multiple Implementation: While C# classes are restricted to single base-class inheritance, a class or struct can implement multiple interfaces, separated by commas.
  • Interface Inheritance: An interface can inherit from one or more other interfaces. The implementing class must fulfill the contracts of the derived interface and all its base interfaces.
  • State: Interfaces absolutely cannot contain instance fields or instance state. While modern C# allows static fields, the prohibition against instance fields remains strictly enforced.

Implicit vs. Explicit Implementation

When a class implements an interface, it can do so implicitly or explicitly. Implicit Implementation exposes the interface members as part of the class’s public API. Explicit Implementation binds the member directly to the interface. The member is not accessible through a class instance; it can only be invoked when the object is cast to the interface type. This is primarily used to resolve naming collisions when a class implements multiple interfaces containing members with identical signatures.
public interface IReadable
{
    void Open();
}

public interface IWritable
{
    void Open();
}

public class FileHandler : IReadable, IWritable
{
    // Explicit implementation of IReadable.Open
    void IReadable.Open()
    {
        // Logic for IReadable
    }

    // Explicit implementation of IWritable.Open
    void IWritable.Open()
    {
        // Logic for IWritable
    }
}

// Usage requires casting:
// FileHandler handler = new FileHandler();
// ((IReadable)handler).Open();

Modern C# Interface Features (C# 8.0+)

Starting with C# 8.0, the CLR and language specifications were updated to support Default Interface Methods (DIM) and additional modifiers.
  • Default Implementations: Interfaces can provide a default body for a method or property. Implementing classes are not required to provide their own implementation for these members. However, if an implementing class relies on the default implementation rather than providing its own, that member is only accessible when the object is cast to the interface type. It is not added to the implementing class’s public API.
  • Access Modifiers: Interface members can utilize access modifiers (private, protected, internal). Unmodified members remain public by default.
  • Static Members: Interfaces can contain static fields, methods, and properties.
using System;

public interface IModernContract
{
    // Standard signature
    void Execute();

    // Default interface method
    public void LogExecution()
    {
        Console.WriteLine("Execution logged via default implementation.");
    }

    // Static member
    public static int MaxRetries = 3;
}
Master C# with Deep Grasping Methodology!Learn More