Interfaces in .Net
What is an Interface?
Interface can be defined as collection of declaration of Properties, Methods, Procedures(VB.Net), Functions and Events. No member of an interface can have a definition, also interfaces cannot be initialized like concrete classes. All declarations of an interface are by default Public, infact Interface itself is Public in .Net and because of this property of an interface , in .net, you do not need to write access modifiers while creating an interface.
Syntax of Interface (C#) --
Syntax of Interface (VB.Net) --
We can provide either Public or Internal(C#)/Friend(VB.Net) access modifier to an interface. Where Internal/Friend will restrict the accessiblity of the interface to the defining assembly only.
Interfaces are implemented not inherited. When a class implements an interface, compiler will force it to implement each and every member of that interface. So we can also say that Interface is kind of contract which any implementing class has to adhere and that class cannot voilate that contract.
So where to use interfaces?
Interfaces are ideally used in situations where you just want to control the definitions of any software. For Example, in a project where core object structure is developed and then business objects are releazed for further development to third parties, it would be good idea to create interfaces for classes and enforce third parties to implement those interfaces. This way all the classes created will adhere to one standard and there will be less chance of design error in project.
Even in cases where single party is involved in development interfaces provide clean and understandable implement of design. Interface can make object structure more understandable.
Microsoft has exposed many interfaces to maintain a uniform implementation, for example IDisposable.
Interface can be defined as collection of declaration of Properties, Methods, Procedures(VB.Net), Functions and Events. No member of an interface can have a definition, also interfaces cannot be initialized like concrete classes. All declarations of an interface are by default Public, infact Interface itself is Public in .Net and because of this property of an interface , in .net, you do not need to write access modifiers while creating an interface.
Syntax of Interface (C#) --
public interface ISampleInterface
{
event EventHandler CustomEvent1;
int MYProp1
{
get;
set;
}
void DoWork1();
}
Syntax of Interface (VB.Net) --
Interface ISampleInterface
Event CustomEvent1 As EventHandler
Property MyProp1() As Integer
Sub DoWork1()
End Interface
We can provide either Public or Internal(C#)/Friend(VB.Net) access modifier to an interface. Where Internal/Friend will restrict the accessiblity of the interface to the defining assembly only.
Interfaces are implemented not inherited. When a class implements an interface, compiler will force it to implement each and every member of that interface. So we can also say that Interface is kind of contract which any implementing class has to adhere and that class cannot voilate that contract.
So where to use interfaces?
Interfaces are ideally used in situations where you just want to control the definitions of any software. For Example, in a project where core object structure is developed and then business objects are releazed for further development to third parties, it would be good idea to create interfaces for classes and enforce third parties to implement those interfaces. This way all the classes created will adhere to one standard and there will be less chance of design error in project.
Even in cases where single party is involved in development interfaces provide clean and understandable implement of design. Interface can make object structure more understandable.
Microsoft has exposed many interfaces to maintain a uniform implementation, for example IDisposable.


0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home