10 November 2011

When to use Interface in C#?

When to use Interface  in C#?

·         Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
·        Structures cannot inherit from classes, but they can implement interfaces.
·         Interfaces are best suited for providing common functionality to unrelated classes.
·    If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. 

Suppose a business man has two accounts:
1.       Business Account
2.        Personal Account
You might create a standard int
erface named IAccount, which included PostInterest and DeductFees methods:
// C#
// Code for the IAccount interface module.
public interface IAccount
   void PostInterest();
   void DeductFees();

Both Business Account and Personal Account inherit the above interface and implement the method. Both Classes have implement two function and the function change frequently according to bank policies. In those situations Interface is used.
When the function frequently changing and the common function is used for unrelated class, Interfaces is used


No comments:

Post a Comment

Comments Welcome