Using Interface in C# Language

Submitted by: 
Language: 

Objective

In this tutorial we will show you about Interaces in C#. This is also important definition, beside abstract and inheritance definition. Interface make your program conform to a clearly structure and defined architecture.

Let's go

What happens if you want some classes conform to a contract, here we mean if you want all the derived class must implement the same members/methods from its parents. You can write a base/parent class and let derived class implements that base class. But if you have more than one base class, so you cannot do that. C# doesn't allow you to implement more than one base class. Alternatively, you can use Inteface.

Let see the code snippet bellow to understand of how to declare an interface:

  1. public interface IMachine
  2. {
  3. //method of the interface
  4. void DoOperation();
  5.  
  6. //method of the interface
  7. void increaseSpeed(int v);
  8.  
  9. //method of the interface
  10. void decreaseSpeed(int v);
  11. }

In above example we declare an Interface named IMachine, with three members (methods) includes DoOperation, increaseSpeed and decreaseSpeed. When a class inherits this interface, it must implement these methods. A class can inherit multiple interface.

If a base class inherits from an interface, any classes that derive from that base class inherit that implementation.

In C# language you need to note that only a class can inherit an interface, an Interface cannot inherit from an other interface. An interface however can inherit from a base interface. In this case, a derived interfacecannot override the members/methods from the base Interface, it only declare a new members.

style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-3664195971165750"
data-ad-slot="8323940646">

In case of a class inherits from multiple interface, the class must implements all members/methods from these interface. A note is that if these interfaces have the same methods/members name, the derived class will must declare the methods/members with the same name. That is true if the members/methods (with the same name) have the same input parameters. Let see an example bellow:

  1. public interface IMachine
  2. {
  3. //method of the interface
  4. void DoOperation();
  5.  
  6. //method of the interface
  7. void increaseSpeed(int v);
  8.  
  9. //method of the interface
  10. void decreaseSpeed(int v);
  11. }
  12.  
  13. public interface ITool
  14. {
  15. //method of the interface
  16. void DoOperation();
  17. }
  18.  
  19.  
  20. public class Car : IMachine, IEquipment
  21. {
  22. public void DoOperation()
  23. {
  24. }
  25.  
  26. public void increaseSpeed(int v)
  27. {
  28. }
  29.  
  30. public void decreaseSpeed(int v)
  31. {
  32. }
  33. }

Here we declare two interfaces that have the same DoOperation method name, and a class named Car that inherits both interface. In this case the compiler makes the public methods match both interface members, and we call it as implicit implementation.

If the two DoOperation above method has difference input parameters, you might want to declare specifically which memebers/methods of the interface you want to implementation in your code. This case you must use Explicit implementaion.Let see how to do that:

  1. public class Car : IMachine, ITool
  2. {
  3. //explicit implementation with interface IEquipment
  4. public void IEquipment.DoOperation()
  5. {
  6. }
  7.  
  8. public void increaseSpeed(int v)
  9. {nc
  10. }
  11.  
  12. public void decreaseSpeed(int v)
  13. {
  14. }
  15. }

Here you see Explicit implementation is very helpful if the two methods with the same name but have different input parameters.

Summary

In this tutorial you have been shown some very helpful information about interface, includes:

  • Definition and how to declare an interface
  • Inherit from multiple interfaces

With interface you can do many things to reuse and make flexibility to you program. Just try to use it in a right way.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.