c sharp

C# Interface

Just like classes, there are some other containers called interfaces which contain methods, events, and properties. Interfaces contain only the declaration and the definition of the functions;the whole description is used somewhere else in the program or mainly in other classes inheriting the interface. Interfaces do not have their private members. All the members of the function description are public and act as abstract information. An interface is defined by using the keyword name interface along with the name you want to give. By using classes, we are unable to perform the phenomenon of multiple inheritances. But by using interfaces it is possible to perform multiple inheritances. The working of the interface will be discussed here.

Syntax to declare an interface

Interface <name_of_interface>

{

<function name>

<events name>

<index name>

}

After defining the interface, now we will implement them by using the following syntax:

# Class x : name_of_interface

This declaration is done by using the name of the interface associated to a class with a colon. By doing this, all the members inside the interface are declared empty A class that implements the interface should implement all the features of the interface.

Implementation of interface

Example 1

We will declare the interface with its name after the declaration of a library. Inside the interface, we will only declare the function name. The return type will also be mentioned. The interface contains only the headers of the function it does not contain the description related to the function. So, we close the interface body because only a single function is defined here.

Interface inter1

{

void display();

}

For the function to get executed, we use a class as a simple c sharp program is declared. But for the interface, the class name is declared along the interface to associate with it.

# Class test class: inter1

Inside the class, the function is written. We have simply used the console statement to display a sample sentence.

In the main program, we will create the object for the class. This declaration is done dynamically with the use of the ‘new’ keyword.

Testclass t = new testclass();this object will be used to call the function declared inside the class after the object is created.

# t.display();

Now we will execute the code by using an MCS compiler and mono to execute the file with the .exe extension.

$ MCS file.cs

$ mono file.exe

On execution, you will see that the sentence is displayed that was declared in the interfaceand executed in a separate function that accessed by the object in the main program.

Example 2

Unlike the first example, we will use three abstract methods in an interface. These functions are related to the vehicle. Each function contains parameters to accept the values that have been sent from the function call.

As the interface is declared, a class is implemented to make use of all the functions inside the interface. We have used two classes that have two different types of vehicles. Each implementing all three functions that are declared inside the interface.

The first function related to gear will assign a new gear value to the previous one by accepting the value from the parameter.

# Gear = newGear;

The second function is about speed up. So, the sent value in the parameter will be added to the previous one.

# Speed = speed + increment;

Contrary to speedup, the brakes function will minus or decrement the sent value from the previous one.

# Speed = speed – decrement;

The values of speed and gear will be displayed through the function. It is not declared in the interface and it is the static function of the class.

Another class is designed to implement the interface. All the approaches for each function are the same as we have described for the first class. Similarly, the display function will display all the data present.

Now, it is time to declare the main program to access each class through the objects respectively. A class sample is created having the main program inside it. As we have two classes to implement the interface, (bicycle and bike) we will create objects separately for each class.

First, for the bicycle:

# Bicycle bicycle = new Bicycle();

The object creation will be dynamically done. Through this object, each function is called.

# Bicycle.changeGear(2);

Each parameter of the function contains the integer type value in the argument. Then each resultant is displayed by calling the display function.

After the bicycle, the instance for the bike will be created.

# Bike bike = new Bike();

Similarly, all function calls will be done through this bike object.

# Bike.speedUp(4);

Now, we will execute the code to see how it works. While executing, both the values for the gear and speed for each class are displayed separately depending on the sequence of object creation and function calling through the object.

Example 3

In the previous example, we have only displayed the values directly in the program that are declared in the interface. But this time, we will calculate the area of a polygon by having the function in two different shapes. Let us have a look at the implementation of the example. First, we will declare the interface here.

Interface Ipolygone

{

Void calculateArea();

}

The function does not contain any parameters with it. This means all the variables are locally defined inside the function. After the interface, we will declare a class rectangle that will implement the Ipolygon interface. So, we will associate the class with the interface name.

# Class rectangle : Ipolygone

Inside the class, the function calculateArea is used. Inside the function, all variables are assigned with a value. These values are then used in the formula to calculate the area of the rectangle.

# int area = l * b;

After the calculations, the resultant value is displayed.

The same phenomenon of implementing the Ipolygon interface is done for the square shape. But as all sides of the square are equal, we do not need two-sided lengths. All sides are of equal length, so the area will be calculated by multiplying any two lengths by one another.

# int area = l * l;

In the main program, the objects for both the classes are created and then the function is called through these objects.

rectangle ri = new rectangle ();

R1.calculateArea();

Same approach goes for the square class.

Now execute the code, you can see that both the areas of different squares are shown by calling the functions through their objects respectively.

Conclusion

C sharp interface is used to hide some specified information in a certain way to display only that information that is important. While defining the interface, we have seen that only the header of the function is described. The header declaration consists of the return type of the function, and the argument that is present in the parameter. In this article, we have implemented some examples in the Linux operating system. In each example, a separate interface is defined that contains a single or number of interfaces that are described later in their respective classes.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.