← Back to C# Course | Chapter 8: Interfaces | Lesson 1 of 6

Interface Basics

An interface is a list of promises, such as 'anything that can fly must have a fly action'. It says what must be done but not how.

In this page:

  1. Interface Basics
Syntax
csharp
interface IInterfaceName
{
    ReturnType MethodName();
}

class ClassName : IInterfaceName
{
    public ReturnType MethodName() { }
}

Interface Basics

An interface declares a contract of members that implementing classes must provide, but supplies no implementation of its own (aside from default methods).

It's declared with interface and conventionally named starting with I, like IShape. Interfaces define *what* a type can do, not *how* it does it.

Note: Interface members are implicitly public — you never write an access modifier on them.

Example: Interface Basics

markup
using System;

interface IGreeter
{
    void Greet();
}

class EnglishGreeter : IGreeter
{
    public void Greet()
    {
        Console.WriteLine("Hello!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IGreeter g = new EnglishGreeter();
        g.Greet();
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to create an object from an interface directly, which is a compile error.
  2. Adding fields to an interface, which is not allowed.
  3. Forgetting that interface members are public by default and adding an access modifier where not needed.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.