Hello Everyone, In this article we will try to understand about interfaces and its use in C#
In above example I have same method declaration in both of my interface but still I don't see any compile time error. .Net framework internally handling this.
In the same way we can use explicit events as well.
Conclusion of the story -
Hope you like reading the article. Please share your comments in case of any confusion.
Interfaces in C# are the empty vocabularies.
What does it mean ? It means it contains only the signature of methods, properties ,events and indexers.
Now the question arises what is the benefit of creating an interface ?
A class that implements interface must implement all the members of interface so basically it creates a responsibility or force implementation to child classes.
How can we create an interface ?
Interfaces are declared using interface keyword. Interface are internal by default.
namespace Interface { public interface IProblem { void MyMethod(); } }
An interface can inherit from one or more interfaces.
public interface IProblem { void MyMethod(); } public interface IProblem1 : IProblem { void MyMethod1(); } public class MyClass : IProblem1 { //Member of IProblem1 public void MyMethod1() { //Some implementation } //Member of IProblem public void MyMethod() { //Some implementation } }
How to Create event member inside Interface ?
public interface IProblem { void MyMethod(); event EventHandler Closed; } public interface IProblem1 : IProblem { void MyMethod1(); } public class MyClass : IProblem1 { event EventHandler myPrivateEvent1; //Member of IProblem1 public void MyMethod1() { throw new NotImplementedException(); } //Member of IProblem public void MyMethod() { throw new NotImplementedException(); } public event EventHandler Closed { add { myPrivateEvent1 += value; } remove { myPrivateEvent1 -= value; } } /// <summary> /// Raising the event. /// </summary> public void OnClosed() { EventHandler handler = myPrivateEvent1; if (handler != null) { handler(null, new EventArgs()); } } }
In C# using interfaces we can achieve multiple inheritance. So it means a class can inherit multiple interfaces. Looks Cool
But What if both of interfaces contains same members - like same method declaration, event declaration. Will it compile ?
Answer is Yes. C# introduces Explicit and Implicit Interface implementations. Above implementations are Implicit implementation and when we explicitly mentioned the name of interface while implementing in a class, It's called Explicit implementation.
Let's see an example -
In above example I have same method declaration in both of my interface but still I don't see any compile time error. .Net framework internally handling this.
But what if I need to implement different behaviours for both of the methods then how can I achieve this ?
See below example-
public interface IProblemNew { void MyMethod(); } public interface IProblem2 { void MyMethod(); } public class NewClass : IProblem2, IProblemNew { void IProblem2.MyMethod() { } void IProblemNew.MyMethod() { } }
So If I am creating object of IProblem2 then in case I should see only the metadata of that interface only. See attached example -
In the same way we can use explicit events as well.
Conclusion of the story -
- It allows force implementation and creates a responsibility on developer to implement all the members of the interface.
- Using interface we can achieve multiple inheritance.
- Interfaces cannot inherit Abstract class.
- Interface can inherit from one or more base interfaces.
- By default interfaces are internal not public.
- We cannot use Sealed keyword while defining interface.
Point to remember - There is some confusion over default access modifier of interface.
Default access modifier of Interface is Internal -
In below example one interface is public and on another one I didn't define any access modifier.
Hope you like reading the article. Please share your comments in case of any confusion.
Happy Coding :)
0 comments:
Post a Comment