Search This Blog

2009-12-01

Tutorial 11:Interfaces

An interface looks like a class, but has no implementation. The only thing it contains are definitions of events, indexers, methods and/or properties.

Defining an Interface: MyInterface.cs

interface IMyInterface
{
void MethodToImplement();
}

Using an Interface: InterfaceImplementer.cs

class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}

public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}
}

Interface Inheritance
using System;

interface IParentInterface
{
void ParentInterfaceMethod();
}

interface IMyInterface : IParentInterface
{
void MethodToImplement();
}

class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
iImp.ParentInterfaceMethod();
}

public void MethodToImplement()
{
Console.WriteLine("MethodToImplement() called.");
}

public void ParentInterfaceMethod()
{
Console.WriteLine("ParentInterfaceMethod() called.");
}
}

No comments: