Interface Definition and Implementation in c#
In the C# programming language, an interface is a method of implementing a class object prototype, which defines a pre-defined set of new methods, properties, events, or indexers for the class, which must be implemented in a user-defined class or structure. A class interface cannot provide any implementations of these class members; it only defines a method signature for the class. Those who implement a user-defined class or its structure’s interface must provide implementations for all class members declared by the class interface.

So, let’s explore the Interface Definition and Implementation concepts in C#.
Interface Definition in C#.
In a C# program, a class interface is declared or defined using the interface keyword. Members in a user-defined class interface are defined as public and abstract by default, meaning that the class does not provide an implementation of the declared interface.
Special Features of C# Interface Implementation.
- No Implementation – A user-defined interface in a class cannot contain class method bodies or fields. A class interface can only define a method signature (e.g., the class method name, return data type, and variables or parameters).
- No Constructor or Destructor – A user-defined interface in a class cannot have a constructor or destructor to construct or destruct objects.
- Implicit Public Members – All members defined in an interface are implicitly public, and C# users cannot use any other class interface access modifiers, such as private, protected, etc.
- Multiple Inheritance – A user-defined class can implement multiple interfaces within a class, unlike an abstract class, which allows C# users to have only a single inheritance feature.
Syntax for defining a class interface in C#.
public interface ICourse
{
void Select(); // Here the class method has no signature, or no body
void Selection(); // Here the method signature, but no body
}
In the above example, the class interface,
- In this syntax, the ICourse interface is a user-defined implementation method.
- It creates two user-defined Select() and Selection() method signatures, which must be implemented by any class implementing this interface.
Interface Implementation in C#.
A user-defined class or struct implements a class interface by providing method bodies for each class method declared in the interface. Where the :interface_name syntax is used in a class to indicate how it implements the interface in the existing class.
Important elements for implementing an interface in C#.
- Provide method implementations – A user-defined program class must provide implementations for all class methods and properties declared in the interface.
- Methods cannot be omitted – If a user-defined class implements an interface in an existing class, the existing program must properly implement all of the interface’s methods, or if the class does not fully implement the interface, it must be marked or indicated as abstract in a class.
Syntax for implementing a class interface in C#.
using System;
public class Java : ICourse
{
// here we are implementing the Select method from the ICourse interface
public void Select()
{
Console.WriteLine(“Java language selected.”);
}
// Here we are implementing the Selection method from ICourse
public void Selection()
{
Console.WriteLine(“Select your desired course…”);
}
}
Here’s an example of implementing a class interface.
- In this program, the class implements the Java ICourse interface.
- It provides robust implementations for both the Select() and Selection() class methods.
Using Interfaces in C#.
When a user-defined class interface is declared and implemented by a class, C# users can create new objects of the class as needed and call the class methods defined by the class interface. Additionally, C# users can use interface type methods to reference objects of the particular implementing class.
Example of using an interface in C#.
using System;
public class program
{
static void Main()
{
// here we Creating an object of the class Java
ICourse course = new Java();
// here it Calling methods defined by ICourse
course.Select(); // Result is – Java language selected.
course.Selection(); // Result is – Select your desire course.
}
}
Here’s an example of using interfaces.
- In this example, an object of type ICourse is created, but it’s actually a Java object.
- Even if the reference is of type ICourse, we can call the Select() and Selection() class methods, because the Java class implements these methods.
Multiple Interface Implementation in C#.
In a C# program, a class or structure can implement more than one class interface, allowing it to hold multiple roles or capabilities within a user-defined class interface. This is a useful feature for grouping behavior from multiple sources into implementations of a class interface.
Example of multiple interface implementation in C#.
using System;
public interface ICourse
{
void Select();
}
public interface ISelectable
{
void Selection();
}
public class Java : ICourse, ISelectable
{
public void Select()
{
Console.WriteLine(“Java language selected.”);
}
public void Selection()
{
Console.WriteLine(“Select your desired course.”);
}
}
public class Program
{
static void Main()
{
Java java = new Java();
java.Select(); // Result is – Java language selected.
java.Selection(); // Result is – Select your desired course.
}
}
Multiple Interface Implementation Explanation.
- In this example, the Java ICourse and ISelectable classes implement both interfaces, which provide implementations for the Select() and Selection() class methods.
- A class in a program can implement any number of interfaces and provide behavior for all of them.
Interface Inheritance in C#.
Interfaces in a user-defined class can inherit properties from other interfaces. When an interface in a C# program inherits class properties from another, the derived subclass interface inherits all the class field member elements of the base interface, and any class that implements the derived or subclass interface must provide implementations for all the class members of both interfaces in that class.
Example of class interface inheritance in C#.
using System;
public interface ICourse
{
void Select();
}
public interface ISelectable
{
void Selection();
}
public interface ISelectableCourse : ICourse, ISelectable
{
void Eligible();
}
public class Java : ISelectableCourse
{
public void Select()
{
Console.WriteLine(“Select your desired course.”);
}
public void Selection()
{
Console.WriteLine(“Java language selected.”);
}
public void Eligible()
{
Console.WriteLine(“You are eligible for the Java course.”);
}
}
public class Program
{
static void Main()
{
Java java = new Java();
java.Select(); // Result is – Select your desired course.
java.Selection(); // Result is – Java language selected.
java.Eligible(); // Result is – You are eligible for the Java course.
}
}
Here’s a case of class interface inheritance.
- In the above interface inheritance, ISelectableCourse inherits class properties from both ICourse and ISelectable.
- Since the Java class implements ISelectableCourse, it must implement all methods from both interfaces, as well as the Eligible() class method defined in ISelectableCourse.
Why use interfaces in C#?
- Decouple code – Interfaces in C# programs allow C# users to decouple program source code. Classes that implement interfaces don’t need to know the specific implementation details of other classes.
- Multiple implementations – Remember, a user-defined class can implement multiple interfaces at once, allowing them to be used in different contexts within the current program if needed, and supporting multiple class interface behaviors at the same time.
- Polymorphism – Interfaces in C# programs provide a mechanism for creating polymorphism. In which a reference to a user-created class interface type can point to objects of different classes that implement the same interface within a class.
- Design contracts – Interfaces in a class program are a process of defining clear object prototypes for class behavior. Interfaces are especially useful in large systems where you need to communicate between different parts of the system through class interfaces, while not knowing much about each other’s internal details.
A detailed summary of interface definition and implementation in C#.
- Interface Definition – In a C# program, a user-defined class defines a class object prototype with interface methods, properties, or events that must be implemented by a user-defined class or structure.
- Implementation – A user-defined class or structure must implement all class field members declared in the interface and provide real source code for these class members.
- Multiple Interfaces – A user-defined class can implement more than one class interface at a time, allowing it to combine multiple individual class behaviors into a single class.
- Interface Inheritance – Interfaces declared in a user-defined class can inherit from other class interface properties and features. This allows a derived or subclass interface to group members by adding them to the base interface.
