What Is Interface?: Attributes (Optional)
What Is Interface?: Attributes (Optional)
An interface defines a contract. A class or struct that implements an interface must adhere
to its contract. An interface may inherit from multiple base interfaces, and a class or
struct may implement multiple interfaces.
Interfaces can contain methods, properties, events, and indexers. The interface itself does
not provide implementations for the members that it defines. The interface merely
specifies the members that must be supplied by classes or structs that implement the
interface.
Syntax in C# :
where:
attributes (Optional)
Additional declarative information. For more information on attributes and
attribute classes, see 17. Attributes.
modifiers (Optional)
The allowed modifiers are new and the four access modifiers.
identifier
The interface name.
base-list (Optional)
A list that contains one or more explicit base interfaces separated by commas.
interface-body
Declarations of the interface members.
Remarks
int y
{
get;
set;
}
}
// Constructor:
public MyPoint(int x, int y)
{
myX = x;
myY = y;
}
// Property implementation:
public int x
{
get
{
return myX;
}
set
{
myX = value;
}
}
public int y
{
get
{
return myY;
}
set
{
myY = value;
}
}
}
class MainClass
{
private static void PrintPoint(IPoint p)
{
Console.WriteLine("x={0}, y={1}", p.x, p.y);
}