Interface Definition
Expand your knowledge of .NET types by learning about interfaces.
We'll cover the following
Interfaces are types that define functionality without any implementation. They describe the necessary methods and properties for a type implementing the interface.
We can think of interfaces as contracts. If our class is implementing an interface, it must have all members the interface defines.
Creating interfaces
Interfaces are reference types. They’re defined in the same way as classes, but use the interface
keyword. Just like classes, they’re internal
by default. We can mark an interface as public
if we want it to be accessible to the external code.
public interface MyInterface
{
}
Additionally, although we can name our interfaces any way we want, it’s considered good practice to begin interface names with the letter I. It makes it easier for us to distinguish between interfaces and other types:
public interface IVehicle
{
}
Interface members
Interfaces can define the following set of members:
- Properties
- Methods
- Events
- Indexers
Note: Beginning with C# , interfaces can also define static members and constants.
Let’s build on the IVehicle
interface we created earlier:
Get hands-on with 1400+ tech skills courses.