0% found this document useful (0 votes)
28 views

What Is Interface?: Attributes (Optional)

An interface defines a contract that classes or structs must adhere to by implementing the members specified in the interface. Interfaces can contain methods, properties, events, and indexers but do not provide implementations. A class or struct implements one or more interfaces and must supply implementations for all members of the interfaces. The interface syntax declares members without implementations, while classes provide the code bodies to fulfill the interface contracts.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

What Is Interface?: Attributes (Optional)

An interface defines a contract that classes or structs must adhere to by implementing the members specified in the interface. Interfaces can contain methods, properties, events, and indexers but do not provide implementations. A class or struct implements one or more interfaces and must supply implementations for all members of the interfaces. The interface syntax declares members without implementations, while classes provide the code bodies to fulfill the interface contracts.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1. What is Interface?

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# :

The declaration takes the following form::

[attributes] [modifiers] interface identifier [:base-list] {interface-


body}[;]

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

An interface can be a member of a namespace or a class and can contain


signatures of the following members:
Methods
Properties
Indexers
Events
An interface can inherit from one or more base interfaces. In the following
example, the interface IMyInterface inherits from two base interfaces, IBase1 and
IBase2:
interface IMyInterface: IBase1, IBase2
{
void MethodA();
void MethodB();
}
Interfaces can be implemented by classes and structs. The identifier of the
implemented interface appears in the class base list. For example:
class Class1: Iface1, Iface2
{
// class members
}
When a class base list contains a base class and interfaces, the base class comes
first in the list. For example:
class ClassA: BaseClass, Iface1, Iface2
{
// class members
}

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 MyPoint contains the property
implementation.
// keyword_interface.cs
// Interface implementation
using System;
interface IPoint
{
// Property signatures:
int x
{
get;
set;
}

int y
{
get;
set;
}
}

class MyPoint : IPoint


{
// Fields:
private int myX;
private int myY;

// 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);
}

public static void Main()


{
MyPoint p = new MyPoint(2,3);
Console.Write("My Point: ");
PrintPoint(p);
}
}
Output
My Point: x=2, y=3

You might also like