In C#, an interface is comparable to a class in that it has properties, methods, and events. An interface, on the other hand, just comprises method definitions and no implementation.
A class that inherits an interface is responsible for implementing all of the interface’s methods and elements.
The term “interface” is used in C# applications to construct an interface. As an example,
//interface c# code example
The C# interface isn’t very user-friendly. In general, interfaces are frequent. We utilize them on a regular basis. As you read this post, you’re probably utilizing at least one interface.
We’ll start with common devices like keyboards, mice, and displays, which serve as interfaces to your operating system in this tutorial.
We’ll also provide you with some instances and discuss some of the hazards. When utilizing C# interfaces, you’ll also learn about industry-standard practices. You should have a good understanding of the C# interface at the end.
Creating a C# Interface
Even if Microsoft is a useful source for a definition of a C# interface, having alternative methods to communicate the notion is helpful. We’ll start with the most basic definition and work our way up from there.
1. Definition from a Textbook
“An interface includes specifications for a collection of linked features that a class, or a struct, can implement,” according to Microsoft’s documentation.
Even though this description of an interface appears simple, it contains a lot of information. So, let’s have a look at it.
2. Unpacking the Interface: Beyond the Textbook
In C#, an interface is a type of code construct. It has “definitions” in the form of method signatures and utilizes the keyword “interface.” Here’s a basic illustration:
We have an interface called IDefinable in this example. The practice for naming interfaces is, to begin with, a capital I, which stands for “interface,” and then add an adjective ending in the suffix –able. It’s also a good idea to have one interface for each file (the same goes for classes and structs).
A type in C# is an interface. Use it the same way you would any other type in method arguments, members, method returns, and properties. Here are some examples of types that use our IDefinable interface:
The public access modifier is usually applied to an interface. An interface’s methods have no access modifiers and are always assumed to be public.
Our example, as you can see, just contains one method: Define. IDisposable, the most often used interface in the language, has only one method signature.
Many interfaces feature several method signatures, as well as properties (which are basically fancy names for getter and setter methods). Properties, events, and indexers can all be defined using interfaces.
If you’re curious, here’s the definition for IDisposable:
When an instance is no longer usable, this interface is utilized by a class that includes certain resource cleanup actions.
Let’s look at some instances of how to utilize an interface now that we have a fundamental knowledge of it. To begin, you must implement the interface.
Implementing C# Interfaces
We’ve seen various interface definition examples. Now it’s time to put ideas into practice in classes.
All this entails is assigning a method to the class that matches the interface’s signature. After that, you may use your class for any calls that the interface it implements makes.
Let’s start with IDisposable because it’s one of the most prevalent interfaces. This example will not be functional, but it will provide you with an understanding of how to utilize an interface.
The UserData class implements IDisposable in this code. Because IDisposable is in the System namespace, we must import it using System; We want to implement the interface, which is indicated by the colon (:) between the class name and the interface.
In general, the pattern is as follows:
Your IDE should provide you with various alternatives for implementing the interface in the class when you finish that section. This saves a lot of time and eliminates errors.
Examine the following possibilities for implementing IDisposable:
Keep in mind that IDisposable is a unique interface. It possesses additional abilities! That’s why the menu says “…with Dispose pattern.” Normally, you would only have two choices:
- Implement interface explicitly
- Implement interface
Important Things to Note About C# Interfaces
- We don’t need to write the public modification because interface methods are public by default. If we type public, the compiler will display an error message stating that the modifier public is invalid.
- In the C# language, there are two types of interfaces: implicit and explicit.
- Multiple interfaces can be implemented by a single class.
- When a class is derived from an interface, the class is obligated to implement all of the Interface’s methods. The compiler will display an error if we do not implement the interface method. Because the PersonalLoan class is inherited from the Iloan interface, it must implement all of the interface methods.
Example of a C# application with an Interface