C# Interfaces
C# Interfaces
1. Let's see the example of interface in C# which has draw() method. Its implementation is
provided by two classes: Rectangle and Circle.
using System;
void draw();
Console.WriteLine("drawing rectangle...");
Console.WriteLine("drawing circle...");
Drawable d;
d = new Rectangle();
1
d.draw();
d = new Circle();
d.draw();
Output:
drawing ractangle...
drawing circle...
2. Implementing an Interface
We cannot create objects of an interface. To use an interface, other classes must implement it. Same
as in C# Inheritance, we use : symbol to implement an interface. For example,
using System;
namespace CsharpInterface {
interface IPolygon {
int area = l * b;
class Program {
2
r1.calculateArea(100, 200);
Output
In the above example, we have created an interface named IPolygon. The interface contains a
method calculateArea(int a, int b) without implementation.
using System;
namespace CsharpInterface {
interface IPolygon {
interface IColor {
void getColor();
int area = a * b;
3
Console.WriteLine("Red Rectangle");
class Program {
r1.calculateArea(100, 200);
r1.getColor();
Output
Red Rectangle
*********************************************************************************
using System;
namespace CsharpInterface {
interface IPolygon {
4
int area = l * b;
class Program {
r1.calculateArea(100, 200);
Output:
In the above example, we have created an interface named IPolygon. The interface contains a
method calculateArea(int l, int b) without implementation.
using System;
namespace CsharpInterface {
interface IPolygon {
void calculateArea();
// implements interface
5
int l = 30;
int b = 90;
int area = l * b;
int l = 30;
int area = l * l;
class Program {
r1.calculateArea();
s1.calculateArea();
Output
6
6. Explicit Interface Implementation Example in C#
using System;
namespace AbstractClassMethods
{
class Program
{
static void Main()
{
ImplementationClass obj1 = new ImplementationClass();
//Using obj1 we can call the Add method directly because
//It is implemented using public access specifier
obj1.Add(10, 20);
//We can call the method directly using the interface reference
//Typecasting is not required in this case
ITestInterface1 obj2 = new ImplementationClass();
obj2.Add(200, 50);
obj2.Sub(200, 50);
Console.ReadKey();
}
}
interface ITestInterface1
{
void Add(int num1, int num2);
void Sub(int num1, int num2);
}
7
void ITestInterface1.Sub(int num1, int num2)
{
Console.WriteLine($"Divison of {num1} and {num2} is {num1 - num2}");
}
}
}
Output:
Sum of 10 and 20 is 30
Declaring Interfaces
Interfaces are declared using the interface keyword. It is similar to class declaration.
Interface statements are public by default. Following is an example of an interface
declaration −
// interface members
void showTransaction();
double getAmount();
Example
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
8
namespace InterfaceApplication {
// interface members
void showTransaction();
double getAmount();
public Transaction() {
amount = 0.0;
tCode = c;
date = d;
amount = a;
return amount;
9
Console.WriteLine("Date: {0}", date);
class Tester {
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
When the above code is compiled and executed, it produces the following result −
Transaction: 001
Date: 8/10/2012
Amount: 78900
Transaction: 002
Date: 9/10/2012
Amount: 451900
10