using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
interface ISampleInterface
{
void SampleMethod();
}
namespace Tutorial
{
class Program : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod()
{
// Method implementation.
Console.WriteLine("Method implementation.");
}
static void Main(string[] args)
{
// Declare an interface instance.
ISampleInterface obj = new Program();
// Call the member.
obj.SampleMethod();
Console.ReadLine();
}
}
}
The following example demonstrates interface implementation. In this example, the interface IPoint contains the property declaration, which is responsible for setting and getting the values of the fields.
The class Point contains the property implementation. random
using System;
using System.Collections.Generic;
using System.Text;
interface IPoint
{
// Property signatures:
int x
{
get;
set;
}
int y
{
get;
set;
}
}// interface
class Point : IPoint
{
// Fields:
private int _x;
private int _y;
// Constructor:
public Point(int x, int y)
{
_x = x;
_y = y;
}
// Property implementation:
public int x
{
get
{
return _x;
}
set
{
_x = value;
}
}
public int y
{
get
{
return _y;
}
set
{
_y = value;
}
}
}// class
namespace IndiLogiX.Tutorial
{
class Program
{
static void PrintPoint(IPoint p)
{
Console.WriteLine("x={0}, y={1}", p.x, p.y);
}
static void Main(string[] args)
{
Point p = new Point(2, 3);
Console.Write("My Point: ");
PrintPoint(p);
Console.ReadLine();
}
}
}