Codecademy Logo

Learn C#: Interfaces

C# Interface

In C#, an interface contains definitions for a group of related functionalities that a class can implement.

Interfaces are useful because they guarantee how a class behaves. This, along with the fact that a class can implement multiple interfaces, helps organize and modularize components of software.

It is best practice to start the name of an interface with “I”.

interface IAutomobile
{
string LicensePlate { get; }
double Speed { get; }
int Wheels { get; }
}
// The IAutomobile interface has three properties. Any class that implements this interface must have these three properties.
public interface IAccount
{
void PayInFunds ( decimal amount );
bool WithdrawFunds ( decimal amount );
decimal GetBalance ();
}
// The IAccount interface has three methods to implement.
public class CustomerAccount : IAccount
{ }
// This CustomerAccount class is labeled with : IAccount, which means that it will implement that interface.

Learn more on Codecademy