Search⌘ K
AI Features

Interface Definition

Explore how interfaces in C# define contracts by specifying methods and properties that implementing classes must fulfill. Learn the syntax for creating interfaces, the role of default implementations to maintain backward compatibility, and why interfaces support multiple behaviors unlike single-inheritance classes. This lesson helps you understand how to design flexible, reusable code using interfaces in object-oriented programming.

Interfaces are types that primarily define a contract. They describe the necessary methods and properties for a type implementing the interface.

We can think of interfaces as a set of rules. If a class implements an interface, it must provide all members the interface defines.

Creating interfaces

Interfaces are reference types. They are defined in the same way as classes, but use the interface keyword. Just like classes, they are internal by default. We can mark an interface as public if we want it to be accessible to external code.

public interface MyInterface
{
}
Defining a simple interface
  • Line 1: We declare a public interface ...