Breaking News
yeni film izle hd film seyret film izle

Interfaces in C#

C# language does not support multiple inheritance. To solve this problem, Microsoft introduced interfaces into C#. They are regarded as an alternative to multiple inheritance. All interfaces should be declared with the keyword Interface.
 
You can implement any number of interfaces in a single derived class. A method declared inside the interface doesn’t contain any method definition. Instead, the implementing class should provide functionality for the corresponding method.
 
For declaring an interface, use the interface keyword instead of class or struct keyword. You can declare the methods inside the interface without any type of access modifier.
 
interface ICompute
{
int ComputeTo(object obj);
} 
 
The example Interface1.cs given below defines an interface named InterfaceDemo is declared with two method definitions. The derived class, InterfaceImp, implements these two methods. Inside the Main () method, an instance of the class is created and the methods are called appropriately.
 
Interface1.cs
 
using System;
interface InterfaceDemo
{
void display();
void print();
}
class InterfaceImp:InterfaceDemo
{
//Interface method implemented
public void display()
{
 	Console.WriteLine("display() method implemented");
}
public void print()
{
 	Console.WriteLine("print() method implemented");
}
public static void Main()
{
 	InterfaceImp ip = new InterfaceImp();
 	ip.display();
 	ip.print();
}
}
 
Combining Interfaces
 
In C# programming language, Microsoft introduces a unique feature with which you can combine interfaces. That means two or more interfaces can be combined into a single interface. After that you only need to call the combined interface while inheriting all of them.
 
In the given example CombiningInterfaces.cs, two interfaces named InterfaceDemo and InterfaceDemo1 are combined into a single interface called CombinedInter. The class MultipleInter extends this interface and provides the implementation of the interface methods. Inside the Main () method, the methods are called by creating an instance of the class.
 
CombiningInterface.cs
 
using System;
interface InterfaceDemo
{
void show();
}
interface InterfaceDemo1
{
void display();
}
interface CombinedInter:InterfaceDemo,InterfaceDemo1 
{
//Above interfaces combined
}
class MultipleInter:CombinedInter
{
public void show()
{
Console.WriteLine("show() method Implemented");
}
public void display()
{
Console.WriteLine("display() method Implemented");
}
public static void Main(string[] args)
{
MultipleInter mi = new MultipleInter();
mi.show();
mi.display();
}
}

Check Also

Exceptions In C#

System.Exception is the base class for all other exceptions. Below this level are the System.ApplicationException …