So, what are interfaces good for if they don't implement functionality? They're great for putting together plug-n-play like architectures where components can be interchanged at will. Since all interchangeable components implement the same interface, they can be used without any extra programming. The interface forces each component to expose specific public members that will be used in a certain way.
Because interfaces must be implemented by derived classes and structs, they define a contract. For instance, if class foo implements the IDisposable interface, it is making a statement that it guarantees it has the Dispose() method, which is the only member of the IDisposable interface. Any code that wishes to use class foo may check to see if class foo implements IDisposable. When the answer is true, then the code knows that it can call foo.Dispose().A simple example for Interfaces:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExInterfaces
{
interface ISampleInterface
{
void samplemethod1();
void samplemethod2();
}
class Class1 : ISampleInterface
{
public void samplemethod1()
{
Console.WriteLine( "sample method 1 is called" );
}
public void samplemethod2()
{
Console.WriteLine("sample method 2 is called");
}
}
class Program
{
static void Main( string[] args )
{
Class1 obj = new Class1();
obj.samplemethod1();
obj.samplemethod2();
Console.ReadLine();
}
}
}
Now that this class inherits the ISampleInterface interface, it must implement all its members. It does this by implementing the samplemethod1() and samplemethod2() methods. Notice that this method implementations has the exact same signature, parameters and method name, as defined in the ISampleInterface interface. Any difference between the method signature in the interface and the method signature in the implementing class or struct will cause a compiler error. Additionally, a class or struct that inherits an interface must include all interface members; You will receive a compiler error if you don't implement all interface members.
Interface Inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
interface IParentInterface
{
void samplemethod1();
}
interface IChildInterface : IParentinterface
{
void samplemethod2();
}
class InterfaceImplementer : IChildInterface
{
public void samplemethod1()
{
Console.WriteLine( "sample method 1 is called" );
}
public void samplemethod2()
{
Console.WriteLine( "sample method 2 is called" );
}
}
class Program
{
static void Main( string[] args )
{
Class1 obj = new Class1();
obj.samplemethod1();
obj.samplemethod2();
Console.ReadLine();
}
}
}
The the above example it contains two interfaces: IChildInterface and the interface it inherits, IParentInterface. When one interface inherits another, any implementing class or struct must implement every interface member in the entire inheritance chain. Since the InterfaceImplementer class inherits from IChildInterface, it also inherits IParentInterface. Therefore, the InterfaceImplementer class must implement the samplemethod2() method specified in the IChildInterface interface and the samplemethod1() method specified in the IParentInterface interface.
No comments:
Post a Comment