Click here to Skip to main content
14,694,106 members
Articles » Languages » C# » Beginners
Technical Blog
Posted 18 Jul 2009

Tagged as


1 view
59 bookmarked

What’s the Deal with Interfaces?

Rate me:
Please Sign up or sign in to vote.
4.48/5 (21 votes)
19 Apr 2010CPOL
Many beginners struggle with the concept of an Interface. Over on the ASP.NET forums, where I moderate, the question is asked a surprising number of times. I'm going to try to describe and explain the concept of an Interface…simply and concisely.

This post is for beginners.

Many beginners struggle with the concept of an Interface. Over on the ASP.NET forums, where I moderate, the question is asked a surprising number of times. I'm going to try to describe and explain the concept of an Interface…simply and concisely.

Let's say we are going to program a game and the game needs a random number generator.

We want to try different random number generators because all random number generators are not created equal. To make it easy to switch number generators, we will use an Interface.

The Interface

So, here is the (simple as possible) Interface for our Random Number Generator:

public interface IRandomNumberGen
    int GetNextNumber(); // note: public is not needed


The public keyword is not needed (or even allowed) on the GetNextNumber() method. An interface declares access methods so by default they must be public. A private method in an interface makes no sense...and causes an error.

An Interface cannot contain fields. Doing so causes an error.

Hmmm, it seems an Interface is a collection of empty functions that are implicitly public.

Derive some Classes from the Interface

Here are two Random Number Generators each inheriting from our Interface.

class RandomNumberCoinFlip : IRandomNumberGen
    public int GetNextNumber()
        // flip a coin for each bit of the number
        return 1;
    private String CoinType; // penny, nickel, etc.

class RandomNumberOuija  : IRandomNumberGen
    public int GetNextNumber()
        // Use a Ouija board to get a number
        return 2;


Both classes implement the GetNextNumber() function. One class has an additional field…but that's OK; it doesn't matter:

As long as a class implements the functions in the Interface, it can do anything else it pleases. But a class derived from an Interface must implement the functions in the Interface.

So big deal, what have we accomplished?

Here are the Classes in Use

protected void Button1_Click(object sender, EventArgs e)
    IRandomNumberGen Generator;

    Generator = new RandomNumberCoinFlip();
    Response.Write(Generator.GetNextNumber() + "<br />");

    Generator = new RandomNumberOuija();
    Response.Write(Generator.GetNextNumber() + "<br />");


The Generator reference can be assigned to any object created from a class derived from the IRandomNumberGen Interface and it can call the GetNextNumber() method. It doesn't know or care about any part of the class except the methods it expects to be there.

Here’s Another Usage Example

public void MakeAMove(IRandomNumberGen NumberGenerator)
    int Number = NumberGenerator.GetNextNumber();
    // do something with Random Number

protected void Button1_Click(object sender, EventArgs e)
    MakeAMove(new RandomNumberCoinFlip());
    MakeAMove(new RandomNumberOuija());


This shows a function that takes an Interface as a parameter. Any object derived from the Interface can be passed to the function.

That's cool but the same thing could be accomplished with an abstract base class and virtual functions. So what's the deal with Interfaces?

Multiple-Inheritance is not Supported in .NET… Except for Interfaces!

Here is one more class that implements the Interface. In addition to IRandomNumberGen, it implements IDisposable.

class RandomNumberDeckOfCards :  IRandomNumberGen, IDisposable
    public int GetNextNumber()
        // Shuffle deck, pick a card
        return 3;
    public void Dispose()
        // free up deck

And here it is being used:

protected void Button1_Click(object sender, EventArgs e)
    IRandomNumberGen Generator;

    Generator = new RandomNumberDeckOfCards();
    Response.Write(Generator.GetNextNumber() + "<br />");

    IDisposable Disposer = Generator as IDisposable;


As before, an IRandomNumberGen reference can be assigned to the object and the GetNextNumber() method called.

In addition, an IDisposable reference can be assigned (with a cast) to the object since it implements IDisposable and the Dispose() method can be called.

In the above code, the two Interface references, Generator and Disposer, both point to the same object. They have different 'views' of the same object. And that, is the deal with interfaces.


I know there are thousands of articles around explaining this concept which is odd because once understood, the concept is trivial. But sometimes one explanation "fits the brain" better than another explanation. I hope this explanation fits someone's brain.


Many have asked for clarification between abstract classes and interfaces. In addition to "multiple inheritance" which abstract classes do not support, there is another key difference: Interface classes may NOT contain fields. If you need fields or properties in the base class, you cannot use interfaces.

Update 2

Strike part of the last update.  You can have properties in an interface but not fields. Thanks to Wallism for pointing this out.

public interface MyInterface
    int LastResult { get; set; }

public class MyClass : MyInterface
    public int LastResult { get; set; }

Steve Wellens Image 1


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

GeneralMy vote of 5 Pin
Nikhil_S12-Sep-12 1:50
professionalNikhil_S12-Sep-12 1:50 
GeneralMy vote of 4 Pin
StianSandberg19-Jul-12 9:59
professionalStianSandberg19-Jul-12 9:59 
GeneralMy vote of 5 Pin
Nikhil_S7-May-12 0:02
professionalNikhil_S7-May-12 0:02 
GeneralMy vote of 5 Pin
jim lahey22-Mar-12 6:51
Memberjim lahey22-Mar-12 6:51 
GeneralMy vote of 5 Pin
dasunnavoda29-Jan-12 17:14
Memberdasunnavoda29-Jan-12 17:14 
GeneralMy vote of 4 Pin
Sandesh M Patil12-Jul-11 2:26
MemberSandesh M Patil12-Jul-11 2:26 
GeneralQuestion for other developers about interfaces Pin
ZeEv 127-Apr-10 0:37
MemberZeEv 127-Apr-10 0:37 
GeneralRe: Question for other developers about interfaces Pin
Steve Wellens27-Apr-10 3:36
MemberSteve Wellens27-Apr-10 3:36 
GeneralRe: Question for other developers about interfaces Pin
ZeEv 127-Apr-10 21:44
MemberZeEv 127-Apr-10 21:44 
GeneralThe way I understand Interfaces Pin
chuckdawit26-Apr-10 20:06
Memberchuckdawit26-Apr-10 20:06 
GeneralRe: The way I understand Interfaces Pin
Steve Wellens27-Apr-10 3:34
MemberSteve Wellens27-Apr-10 3:34 
GeneralAs a begginer, I liked it Pin
Hallmanac26-Apr-10 17:15
MemberHallmanac26-Apr-10 17:15 
Generalinherit an interface.. Pin
Andrei Ion Rînea23-Apr-10 1:08
MemberAndrei Ion Rînea23-Apr-10 1:08 
GeneralRe: inherit an interface.. Pin
Steve Wellens23-Apr-10 2:58
MemberSteve Wellens23-Apr-10 2:58 
I think the terminology is OK.

Here's some of Microsoft's latest documentation:

"When a class or struct is said to inherit an interface, it means that the class or struct provides an implementation for all of the members defined by the interface."[^]

If you can convince Microsoft to change their language, I will conform since they are the creators/owners of C#.
Steve Wellens

GeneralWish you added some more Pin
larry11820-Apr-10 12:23
Memberlarry11820-Apr-10 12:23 
Generalproperties Pin
wallism19-Apr-10 13:47
Memberwallism19-Apr-10 13:47 
GeneralRe: properties Pin
Steve Wellens19-Apr-10 15:47
MemberSteve Wellens19-Apr-10 15:47 
Generala "touchy" subject. Pin
Diamonddrake13-Apr-10 13:37
MemberDiamonddrake13-Apr-10 13:37 
GeneralRe: a "touchy" subject. Pin
Steve Wellens13-Apr-10 13:48
MemberSteve Wellens13-Apr-10 13:48 
GeneralMy vote of 1 Pin
Roms4-Feb-10 7:31
MemberRoms4-Feb-10 7:31 
GeneralRe: My vote of 1 Pin
jim lahey22-Mar-12 6:50
Memberjim lahey22-Mar-12 6:50 
GeneralIt fits my brain Pin
baiyuxiong9-Dec-09 3:59
Memberbaiyuxiong9-Dec-09 3:59 
GeneralRe: It fits my brain Pin
WesMcGJr20-Apr-10 5:13
MemberWesMcGJr20-Apr-10 5:13 
GeneralRe: It fits my brain Pin
Hezek1ah27-Apr-10 6:46
MemberHezek1ah27-Apr-10 6:46 
GeneralCould help some... Pin
CurtainDog21-Jul-09 3:37
MemberCurtainDog21-Jul-09 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.