What is an Interface?
Interface is defined as set of methods properties evnets and fields.
The scope of an inetrface is public.interface contain only signature.(it contain only definition no implementation)
Implementation are done in derived class.Only the interface can support multiple inheritance in C#.
//Example of Interface
interface ITestInterface
{
void TestMethod();
//Declaring Properties in interface
int Count
{
get;
set;
}
//Read only Property
string Item
{
get;
}
//Write only Property
string Name
{
set;
}
}
//interface Implementation are done in derived class.so we are going to inherit ITestInterface in a class named InterfaceImplimentation
public class InterfaceImplimentation:ITestInterface
{
public InterfaceImplimentation()
{
}
#region ITestInterface Members
public void TestMethod()
{
//Write Implimentation
}
private int y;
private string z = string.Empty;
public int Count
{
get
{
return y;
}
set
{
y = value;
}
}
public string Item
{
get
{
return "Item1";
}
}
public string Name
{
set
{
z = value;
}
}
#endregion
}
//implimentation
public class Implementation
{
InterfaceImplimentation if1 = new InterfaceImplimentation();
public void GetItem()
{
if1.TestMethod();
}
}