In this article, we will look at the common things that need to be done to declare and implement an interface.
An interface looks like a class that can have a set of properties, methods, events and indexers, but has no implementation. The interface does not have an implementation of properties, methods, events and indexers because they are inherited by classes and structs, which must provide an implementation for each interface member.
An interface can be used to hide implementation details of classes from each other and it allows various objects to interact easily. It provides a separation between interface and implementation.
An interface is not only a contractual obligation to implement certain methods, properties, events and indexes but also provides a very low coupling between two classes. Interfaces in C# are provided as a replacement of multiple inheritances of classes because C# does not support multiple inheritances of classes.
Interfaces and abstract classes have similar purposes. Generally, an interface should be used when the implementation of the members it defines is not fixed, or when it is known that a single class requires the contents of several interfaces. Where the functionality of some members is fixed and the limited multiple inheritance facility is not required, abstract classes may be more appropriate.
How to create an Interface
Syntax
Interface interface_name
{
}
1. An interface defines a signature of methods, events and indexes but not an implementation.
Example: An interface IVehicle that has a method "Speed()" with its definition/implementation.
interface IVehicle
{
double Speed(int distance, int hours)
{
double speed = 0.0d;
speed = distance / hours;
return speed;
}
}
We execute the above code and get an error such as:
Error : 'IVehicle.Speed(int, int)': interface members cannot have a definition
So the interface has only signature of methods, no implementation.
2. Interface members are public by default; there is no need to use an access specifier.
Example
An interface IVehicle that has a method "Speed()" signature and has a public access specifier.
interface IVehicle
{
public double Speed(int distance, int hours);
}
We execute the above code and get an error such as:
Error: The modifier 'public' is not valid for this item
So the interface members don't have any access specifier, by default they are public.
3. By default an interface is internal and we can't declare it private, protected or protected internal because it is always inherited by classes or structs.
Example: An interface IVehicle that has a private access specifier:
private interface IVehicle
{
double Speed(int distance, int hours);
}
We execute the above code and get an error such as:
Error: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
So by default the interface itself is internal and we can also use a public access specifier but can't use private, protected and protected internal. One more thing is that class and struct also are by default internal as well as interfaces.
4. An interface does not have fields; in other words we can't declare variables in an interface.


Sam HobbsPosted Mar 14, 2024, 2:55 PM
When you say "execute the above code" and you get an error, are you sure the program is compiled successfully and then executes? I assume the errors are compiler errors, not execution errors.
Gowtham AllakaPosted Jun 4, 2018, 5:25 AM
Nice article
Rakhi khatriPosted Apr 8, 2017, 12:56 PM
Thanku sir nice to understand for me
Vinay KPosted Mar 15, 2017, 6:34 AM
Thank you very much Sandeep Singh
Vinay KPosted Mar 15, 2017, 6:34 AM
Easy to understand....
Rajeesh MenothPosted Nov 7, 2015, 6:29 AM
Good Explanation :)
Jitendra Singh PatelPosted Apr 30, 2015, 10:24 PM
it's nice information about interface
Sachin kumar PandeyPosted Apr 9, 2015, 7:10 AM
Nice article dude , it's very helpful.
Sandeep Singh ShekhawatPosted Sep 6, 2013, 10:03 AM
Thanks Pavan
Pavan RamamurthyPosted Sep 6, 2013, 1:30 AM
good collection....