Interface:
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
Example:
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
Example:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication1
{
interface ICanBeUsedAsAChair //Declare
interface
{
void
SittingMessage();
}
public class MilkCrate : ICanBeUsedAsAChair
{
public
MilkCrate()
{
Console.WriteLine("I am Milk Crate");
}
public void SittingMessage()
{
Console.WriteLine("You can sit on me i am Crate.");
}
}
public class Table : ICanBeUsedAsAChair
{
public
Table()
{
Console.WriteLine("I am Table");
}
public void SittingMessage()
{
Console.WriteLine("You can sit on me i am Table.");
}
}
class Program
{
public static void CanSitOn(ICanBeUsedAsAChair fakeChair) {
fakeChair.SittingMessage();
}
static void Main()
{
MilkCrate
milkCrate = new MilkCrate();
Program.CanSitOn(milkCrate);
Table
table = new Table();
Program.CanSitOn(table);
//Table
table = new Table();
//table.SittingMessage();
//ICanBeUsedAsAChair
sittingObject = (ICanBeUsedAsAChair)table;
//sittingObject.SittingMessage();
//ICanBeUsedAsAChair
sittingObject2 = new Table();
//sittingObject2.SittingMessage();
Console.ReadLine();
}
}
}
Reference: MSDN and youtube.