Hello guys,
An interface in C# is usually used to create loosely-coupled and contract-based designs. It can contain signatures (declarations) of the Methods, Properties, Indexers and Events. The implementation of the methods/properties and so on is done in the class that implements the interface. An interface can inherit one or more interfaces, in other words it supports multiple inheritance whereas classes don’t.
Implicit interface implementation
- interface ITest
- {
- void TestMethod();
- }
- class TestClass: ITest
- {
- public void TestMethod()
- {
- Console.WriteLine(“Implicit Interface Implementation”);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- TestClass obj = new TestClass();
- obj.TestMethod(); //Way to call implicitely implemented method
- }
- }
Explicit interface implementation
This is another way to implement members of an interface. Here we need to specify the interface name of the members. The following example explains that.
- class TestClass: ITest
- {
- void ITest.TestMethod()
- {
- Console.WriteLine(“Explicit Interface Implementation”);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ITest obj2 = new TestClass();
- obj2.TestMethod();
- }
- }

Leave a comment