Fremus.co.za

Demistifying Life and Web Development

C# Interfaces

Okay so what are interfaces and what makes them different from classes? Well an interface is like a contract for a class, that tell that class it must implement all the methods, properties, events and indexers of the named interface. Lets say you create a simple class and a simple interface like this:

class myTestClass : IMyTestInterface
{
       public myTestClass()
       {
       }
}
interface IMyTestInterface
{
        void DoMyFunction();
        void DoMySecondFunction();
}

You will get a compiler error, because MyTestClass is required to implement DoMyFunction and DoMySecond. So what are interfaces used for and what makes it better than just a regular class. Well for one, you can implement multiple interfaces, and as you may know C# does not support multiple inheritance. You can for example implement an interface from one or more other interfaces.

  • Share/Bookmark
posted by fr3dr1k in C# and have No Comments
Tags:

Place your comment

Please fill your data and comment below.
Name
Email
Website
Your comment
Get Adobe Flash playerPlugin by wpburn.com wordpress themes