Introduction to interface
- ”Interface” is the keyword .
- Interfaces define properties and methods, which are the members of the interface.
- Interfaces contains only the declaration of the members in the programming language.
Why use an interface
- Interface is used instead of multiple inheritance.
- Methods are to be declared by the base class and the deriving class implements the functionalities.
Use of an interface
- It is the responsibility of the deriving class to define the members.
- It provides a standard structure.
Description
- In this blog, we learn about the interface concept in C# programming language
- To perform arithmetic operations of addition, subtraction ,Multiplication and division, we use interface concept in C# programming language.
REQUIREMENTS
- Visual Studio 2010.
- Windows operating system Windows 7,8,10.
The code of a program is given below.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace arithmeticinterface {
- public interface interex {
- void add(int a, int b);
- void sub(int a, int b);
- void mul(int a, int b);
- void divi(double a, double b);
- void display();
- }
- class inter1: interex {
- int x, y, z;
- double d;
- public void add(int a, int b) {
- int m, n;
- m = a;
- n = b;
- x = m + n;
- }
- public void sub(int a, int b) {
- int m, n;
- m = a;
- n = b;
- y = a - b;
- }
- public void mul(int a, int b) {
- int m, n;
- m = a;
- n = b;
- z = a * b;
- }
- public void divi(double a, double b) {
- double m, n;
- m = a;
- n = b;
- d = a / b;
- }
- public void display() {
- Console.WriteLine("Addition value is:" + x);
- Console.WriteLine("Subtraction value is:" + y);
- Console.WriteLine("Multiplication value is:" + z);
- Console.WriteLine("Divition value is:" + d);
- }
- }
- class Program {
- static void Main(string[] args) {
- inter1 obj = new inter1();
- int g, h;
- Console.WriteLine("Enter the first Number to perform Arithmetic operations:");
- g = Convert.ToInt16(Console.ReadLine());
- Console.WriteLine("Enter the second Number to perform Arithmetic operations:");
- h = Convert.ToInt16(Console.ReadLine());
- obj.add(g, h);
- obj.sub(g, h);
- obj.mul(g, h);
- obj.divi(g, h);
- obj.display();
- Console.ReadKey();
- }
- }
- }
The output is given below.
Conclusion
I assured that after reading this blog, you all know about an interface in C# programming language.