google search

Friday, February 25, 2011

Interface

An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface, as shown in the following example:

 

 

      interface ISampleInterface

{

    void SampleMethod();

}

 

class ImplementationClass : ISampleInterface

{

    // Explicit interface member implementation:

    void ISampleInterface.SampleMethod()

    {

        // Method implementation.

    }

 

    static void Main()

    {

        // Declare an interface instance.

        ISampleInterface obj = new ImplementationClass();

 

        // Call the member.

        obj.SampleMethod();

    }

}

 

An interface can be a member of a namespace or a class and can contain signatures of the following members:

 

Methods

Properties

Indexers

Events

 

An interface can inherit from one or more base interfaces.

 

When a base type list contains a base class and interfaces, the base class must come first in the list.

 

A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface

 

No comments: