What is an Interface in C#.NET?
Interfaces are just like as abstract classes, but without implementation in it, means only signature of the members will allow in Interface. An abstract can allows both concrete and abstract members in it but Interface only allow abstract members in it.
An interface can contain only signature of classed, method, Indexers, delegates or events. The reason Interface only provide signature is they are inherited by classed, method, Indexers, delegates or events, which must provide the implementation for the members declared in the Index.
The key word Interface is used to create an interface. Below is the code for defining the sample interface.
interface IMySampleInterface
{
void MySampleMethod();
}
The following code demonstrates the Interface implementation. Below IMyValue interface define the signature for the properties for the val1 and val2, these properties need to set the get and set the properties. MyDerived class implemented these properties.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
interface IMyValue
{
int val1
{
get;
set;
}
int val2
{
get;
set;
}
}
class MyDerived : IMyValue
{
private int _val1;
private int _val2;
public MyDerived(int x, int y)
{
_val1 = x;
_val2 = y;
}
// Property implementation:
public int val1
{
get
{
return _val1;
}
set
{
_val1 = value;
}
}
public int val2
{
get
{
return _val2;
}
set
{
_val2 = value;
}
}
}
class MainClass
{
static void MyPrintPoint(IMyValue p)
{
Console.WriteLine("x={0}, y={1}", p.val1, p.val2);
}
static void Main()
{
MyDerived p = new MyDerived(2, 3);
Console.Write("My Point: ");
PrintPoint(p);
}
}
}
Basically, C# does not support Multiple Inheritance, but we can achieve by using Interfaces. Below is the sample program to achieve the multiple inheritances in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
interface IScool
{
void Test();
}
interface IClass
{
void Test1();
}
public class Exam :IClass, IScool
{
void IScool.Test()
{
Console.WriteLine("This is the Scool TEst");
}
void IClass.Test1()
{
Console.WriteLine("This is the Class TEst");
}
}
class MainClass
{
static void Main()
{
}
}
}
Interfaces are much like abstract classes and they share the fact that no instances of them can be created. However, interfaces are even more conceptual than abstract classes, since no method bodies are allowed at all. So an interface is kind of like an abstract class with nothing but abstract methods, and since there are no methods with actual code, there is no need for any fields. Properties are allowed though, as well as indexers and events. You can consider an interface as a contract - a class that implements it is required to implement all of the methods and properties. However, the most important difference is that while C# doesn't allow multiple inheritance, where classes inherit more than a single base class, it does in fact allow for implementation of multiple interfaces.