0% found this document useful (0 votes)
108 views

C# Interfaces

The document discusses interfaces in C# with several examples. It shows how to define an interface with a method, have multiple classes implement that interface by providing an implementation of the method, use reference variables of the interface type to call methods on implementing classes, and implement multiple interfaces in a single class. It also demonstrates explicitly implementing an interface method.

Uploaded by

Balakumar Bk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

C# Interfaces

The document discusses interfaces in C# with several examples. It shows how to define an interface with a method, have multiple classes implement that interface by providing an implementation of the method, use reference variables of the interface type to call methods on implementing classes, and implement multiple interfaces in a single class. It also demonstrates explicitly implementing an interface method.

Uploaded by

Balakumar Bk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C# interface example

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;

public interface Drawable

void draw();

public class Rectangle : Drawable

public void draw()

Console.WriteLine("drawing rectangle...");

public class Circle : Drawable

public void draw()

Console.WriteLine("drawing circle...");

public class TestInterface

public static void Main()

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 {

// method without body

void calculateArea(int l, int b);

class Rectangle : IPolygon {

// implementation of methods inside interface

public void calculateArea(int l, int b) {

int area = l * b;

Console.WriteLine("Area of Rectangle: " + area);

class Program {

static void Main (string [] args) {

Rectangle r1 = new Rectangle();

2
r1.calculateArea(100, 200);

Output

Area of Rectangle: 20000

In the above example, we have created an interface named IPolygon. The interface contains a
method calculateArea(int a, int b) without implementation.

3. Implementing Multiple Interfaces

Unlike inheritance, a class can implement multiple interfaces. For example,

using System;

namespace CsharpInterface {

interface IPolygon {

// method without body

void calculateArea(int a, int b);

interface IColor {

void getColor();

// implements two interface

class Rectangle : IPolygon, IColor {

// implementation of IPolygon interface

public void calculateArea(int a, int b) {

int area = a * b;

Console.WriteLine("Area of Rectangle: " + area);

// implementation of IColor interface

public void getColor() {

3
Console.WriteLine("Red Rectangle");

class Program {

static void Main (string [] args) {

Rectangle r1 = new Rectangle();

r1.calculateArea(100, 200);

r1.getColor();

Output

Area of Rectangle: 20000

Red Rectangle

In the above example, we have two interfaces, IPolygon and IColor.

*********************************************************************************

4. Using reference variable of an interface

We can use the reference variable of an interface. For example,

using System;

namespace CsharpInterface {

interface IPolygon {

// method without body

void calculateArea(int l, int b);

class Rectangle : IPolygon {

// implementation of methods inside interface

public void calculateArea(int l, int b) {

4
int area = l * b;

Console.WriteLine("Area of Rectangle: " + area);

class Program {

static void Main (string [] args) {

// using reference variable of interface

IPolygon r1 = new Rectangle();

r1.calculateArea(100, 200);

Output:

Area of Rectangle: 20000

In the above example, we have created an interface named IPolygon. The interface contains a
method calculateArea(int l, int b) without implementation.

5. Area of Rectangle & Area of Square using interface

using System;

namespace CsharpInterface {

interface IPolygon {

// method without body

void calculateArea();

// implements interface

class Rectangle : IPolygon {

// implementation of IPolygon interface

public void calculateArea() {

5
int l = 30;

int b = 90;

int area = l * b;

Console.WriteLine("Area of Rectangle: " + area);

class Square : IPolygon {

// implementation of IPolygon interface

public void calculateArea() {

int l = 30;

int area = l * l;

Console.WriteLine("Area of Square: " + area);

class Program {

static void Main (string [] args) {

Rectangle r1 = new Rectangle();

r1.calculateArea();

Square s1 = new Square();

s1.calculateArea();

Output

Area of Rectangle: 2700

Area of Square: 900

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 need to typecast obj1 to ITestInterface1 to call the Sub


//method because Sub method is implemented using Interface name
((ITestInterface1)obj1).Sub(100, 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);
}

public class ImplementationClass : ITestInterface1


{
//Interface Method Implementation
public void Add(int num1, int num2)
{
Console.WriteLine($"Sum of {num1} and {num2} is {num1 + num2}");
}

//This method purely belongs to ImplementationClass

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

Division of 100 and 20 is 80

Sum of 200 and 50 is 250

Division of 200 and 50 is 150

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 −

public interface ITransactions {

// interface members

void showTransaction();

double getAmount();

Example

7. The following example demonstrates implementation of the above interface −

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System;

8
namespace InterfaceApplication {

public interface ITransactions {

// interface members

void showTransaction();

double getAmount();

public class Transaction : ITransactions {

private string tCode;

private string date;

private double amount;

public Transaction() {

tCode = " ";

date = " ";

amount = 0.0;

public Transaction(string c, string d, double a) {

tCode = c;

date = d;

amount = a;

public double getAmount() {

return amount;

public void showTransaction() {

Console.WriteLine("Transaction: {0}", tCode);

9
Console.WriteLine("Date: {0}", date);

Console.WriteLine("Amount: {0}", getAmount());

class Tester {

static void Main(string[] args) {

Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);

Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);

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

You might also like