An interface is a very important part of the polymorphism. We know that in polymorphism architecture, it works with base class and child class. In this architecture, base class has some methods and their methods are implemented by child class and they change their behavior at runtime. So we are creating an Abstract class or method. It means this abstract class is implemented by other child classes. In this base class just provide an abstraction not an execution. But partially in this provide some information details shown in the below Code sample,

But pure polymorphism avoids this type of information. So you can’t implement an execution in a base class.
You can see here more detail: What is an abstract class?
That’s for providing Interface with Microsoft Csharp. Interface is a complete abstract environment, because it doesn't implement or execute a method, an event, a property.
So In a Csharp it provides an Interface. An interface is a pure polymorphism. An interface has a complete abstract environment. Therefore you cannot impalement or execute a method, event and property.
What is an Interface?
Interface is like as a class. We create a class the same way that we create an interface. An interface is on;y a type; it doesn't have a UI or design.
First we create a folder with suitable name (like interface) in our project solution explorer and Create Interface inside our newly created folder as shown below,

In the above figure, I am creating an interface and giving the name Ivehicle.cs. In this name first prefix capital letter as per polymorphism rules and always remember these rules for creating the interface.

In the above figure we created an Interface. In an Interface we can create a method, a property and an event as per our code requirement. But we cannot implement or execute creating a method, a property and an event.
We can create a method, a property and an event in an interface but not implement it. So we have raised a question, how are those implement edor executed? This type of implementation is possible through inheritance.
When we create any method or variable in an interface then we do not need to any access specifiers. Because all are by default public.
Now I am creating a method as shown in the below image,

See the above figure -- I am just creating a method not giving details. If you can try to give details then create an error as shown in the below figure,

So we do not give any type of details in the interface, because interface is all pure abstract code.
Now we are creating a class shown in the below figure,

Here we are seeing how to create a car class normally inherited by Ivehicle. Now we are moving our cursor on Ivehicle and we are shown two options of interface as shown in the below figure.

First we are clicking option number one to Implement interface and then see what happens as shown in the below figure,

When we are clickig the first option we create base class Run() method implement as shown in the above figure. Here you are noting we are not using an override keyword. Keep in mind an Override keyword is used in two situations, when base method is defined with virtual or abstract keywords. Now write some details in car class run method as shown in below figure,

Now we are also using one more class, Bus, as similar class car and it also is inherited by Ivehicle.
Now we are moving to program main method as shown in the below figure,

Now we are adding our project namespace in program main method as shown in below figure,

In main method define a variable of Ivehicle as shown in below figure.
Note
We know that at compile time we cannot create an object of abstract class but in an abstract class object creates on runtime. But in an interface we cannot create an object on runtime and compile time, because interface is a pure polymorphism or pure abstraction.

In this above figure, we are creating an object car class and bus class because both are child class and run method is implemented in this child classes. When we run our code the first call car executes and the second time the called bus executes.
Ivehicle.cs Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace OPPs.Interface
- {
- interface Ivehicle
- {
- void Run(); // Create Method
- }
- // Create class car
- class car : Ivehicle // inherit class
- {
- public void Run() // method implement
- {
- Console.WriteLine("Run Car");
- }
- }
- // Create class Bus
- class Bus : Ivehicle // inherit class
- {
- public void Run() // method implement
- {
- Console.WriteLine("Run Bus");
- }
- }
- }










Shovan SahaPosted May 4, 2017, 12:46 PM
This is nice. But I have another query. I have written an application through C# in visual studio 2015 with MS Access database. I want to use this in another computer which has no connection on LAN or Internet. How can i use this in another computer?
Manav PandyaPosted May 3, 2017, 6:01 AM
Nice share ,,,,,,,,