CSharp - Create your own interface

Introduction

An interface is a special type in C#.

An interface contains only method signatures to define some contract.

The implementers need to follow those specifications.

interface declares what we are trying to implement.

interface methods are declared without a body.

The keyword interface is used to declare an interface type.

There is no access modifier attached with an interface method.

It is recommended that you preface the interface name with the capital letter I, such as

interface IMyInterface{
...
}

Demo

using System;
interface IMyInterface
{
    void Show();//from w  ww  . j  ava 2  s . c  o  m
}
class MyClass : IMyInterface
{
    public void Show()
    {
        Console.WriteLine("MyClass.Show() is implemented.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyClass myClassOb = new MyClass();
        myClassOb.Show();
    }
}

Result

Note

To implement an interface, match the method signatures.

Related Topic