Interface in C# Language-: 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.
in other words we can say that the An interface is used to specify members that deriving classes must implement. They are defined with the interface keyword followed by a name and a code block. Their naming convention is to start with a capital I and then to have each word initially capitalized.
interface IMyInterface {}
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.
Interface declaration -: An interface-declaration consists of an optional set of attributes It has the following types
- interface-modifier-:An interface-declaration may optionally include a sequence of interface modifiers.
- partial modifier-: It indicates that this interface-declaration is a partial type declaration. Multiple partial interface declarations with the same name within an enclosing namespace or type declaration combine to form one interface declaration
Interface Signatures
The interface code block can only contain signatures, and only those of methods, properties, indexers, and events. The interface members cannot have any implementations. Instead, their bodies are replaced by semicolons. They also cannot have any access modifiers since interface members are always public.
interface IMyInterface
{
// Interface method
int GetArea();
// Interface property
int Area { get; set; }
// Interface indexer
int this[int index] { get; set; }
// Interface event
event System.EventHandler MyEvent;
}
Interface Example
In the following example, an interface called IComparable is defined with a single method named Compare.
interface IComparable
{
int Compare(object o);
}
The class Circle defined next implements this interface, by using the same notation as is used for inheritance. The Circle class must then define the Compare method, which for this class will return the difference between the circle radiuses. The implemented member must be public, in addition to having the same signature as the one defined in the interface.
class Circle : IComparable
{
int r;
public int Compare(object o)
{
return r – (o as Circle).r;
}
}
Although a class can only inherit from one base class, it may implement any number of interfaces. It does so by specifying the interfaces in a comma-separated list after the base class.
Functionality Interface
IComparable demonstrates the first use of interfaces, which is to define a specific functionality that classes can share. It allows programmers to use the interface members without having to know the actual type of a class. To illustrate, the following method takes two IComparable objects and returns the largest one. This method will work for any two objects of the same class that implement the IComparable interface, because the method only uses the functionality exposed through that interface.
static object Largest(IComparable a, IComparable b)
{
return (a.Compare(b) > 0) ? a : b;
}
Class Interface-:
A second way to use an interface is to provide an actual interface for a class, through which the class can be used. Such an interface defines the functionality that programmers using the class will need.
interface IMyClass
{
void Exposed();
}
class MyClass : IMyClass
{
public void Exposed() {}
public void Hidden() {}
}
The programmers can then view instances of the class through this interface, by enclosing the objects in variables of the interface type.
IMyInterface m = new MyClass();
This abstraction provides two benefits. First, it makes it easier for other programmers to use the class since they now only have access to the members that are relevant to them. Second, it makes the class more flexible since its implementation can change without being noticeable by other programmers using the class, as long as the interface is followed.
Base Interfaces
An interface can inherit from zero or more interface types, which are called the explicit base interfaces of the interface. When an interface has one or more explicit base interfaces, then in the declaration of that interface, the interface identifier is followed by a colon and a comma-separated list of base interface types.
interface member
The members of an interface are the members inherited from the base interfaces and the members declared by the interface itself. example of
public interface IStringList
{
void Add(string s);
int Count { get; }
event StringListEvent Changed;
string this[int index] { get; set; }
}
Interface Properties-: Interface properties are declared using interface-property-declarations.
Interface methods-: Interface methods are declared using interface-method-declarations.
Interface events-:Interface events are declared using interface-event-declarations.The attributes, type, and identifier of an interface event declaration have the same meaning as those of an event declaration
Interface indexers-: Interface Index are declared using interface-indexer-declarations.The attributes, type, and formal-parameter-list of an interface indexer declaration have the same meaning as those of an indexer declaration in a class
Interface member access-:Interface members are accessed through member access and indexer access expressions of the form I.M and I[A], where I is an interface type; M is a method, property, or event of that interface type; and A is an indexer argument list.