In this post we will discuss about interfaces in C#.Net.
Also check out:
- How to use BalloonPopupExtender ajax control in asp.net?
- Working with enterprise library for data access in asp.net
- Null Coalescing Operator in C#.Net
- An interface looks like a class, but has no implementation. An interface describes methods, properties and events.
- A class can implement more than one interface
- C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.
- To declare a interface we have to use the keyword interface like below:
interface ITestInterface
{
void TestMethodToImplement();
}
As the declaration suggest, As a naming convention you can use prefix with I in the interface name like ITestInterface.
- Interface contains unimplemented methods.
- Interfaces must be implemented by derived classes and structs. Example below:
Class myClass : ITestInterface
{
void TestMethodToImplement()
{
//Method body will contain.
}
}
- The class implements the interface must have to implement its members else the class has to be declared as Abstract class.
- Interfaces may also be inherited by other interface.
Below is a way how a class can implement multiple interface:
Class myClass : ITestInterface, IInterface2
{
void TestMethodToImplement()
{
//Method body will contain.
}
void TestMethodInterface2()
{
//Method body will contain.
}
}
Also check out:
- How to use BalloonPopupExtender ajax control in asp.net?
- Working with enterprise library for data access in asp.net
- Null Coalescing Operator in C#.Net
- An interface looks like a class, but has no implementation. An interface describes methods, properties and events.
- A class can implement more than one interface
- C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.
- To declare a interface we have to use the keyword interface like below:
interface ITestInterface
{
void TestMethodToImplement();
}
As the declaration suggest, As a naming convention you can use prefix with I in the interface name like ITestInterface.
- Interface contains unimplemented methods.
- Interfaces must be implemented by derived classes and structs. Example below:
Class myClass : ITestInterface
{
void TestMethodToImplement()
{
//Method body will contain.
}
}
- The class implements the interface must have to implement its members else the class has to be declared as Abstract class.
- Interfaces may also be inherited by other interface.
Below is a way how a class can implement multiple interface:
Class myClass : ITestInterface, IInterface2
{
void TestMethodToImplement()
{
//Method body will contain.
}
void TestMethodInterface2()
{
//Method body will contain.
}
}