Interface is understood as the syntax interface that all classes inheriting from it should follow. Refer to the article below from Mytour to learn more about what Interface means in C#.
What exactly is Interface in C#?
1. What is Interface in C#?.
2. Syntax for Declaring Interface.
3. Syntax for Implementing Interface.
4. Conclusion.
1. What is Interface in C#?
Similar to a class, an Interface can contain methods, properties, events, and indexers as members. However, an Interface only contains declarations of its members, and the implementation of Interface members will be carried out by a class implementing the Interface either implicitly or explicitly.
- Interface defines what a class must do and must not do.
- Interface has no members of its own.
- By default, all members of an Interface are public and abstract.
- Interface is always defined using the 'interface' keyword.
2. Syntax for Declaring Interface
Declaration Syntax of Interface in C#:
interface
{
// declare events
// declare indexers
// declare methods
// declare properties
}
3. Syntax for Implementing Interface
Below is the syntax for implementing Interface: class class_name: interface_name
To declare an Interface, we use the Interface keyword. This keyword is used to declare public and abstract members. This means that all members in the interface are declared with an empty body, and are public and abstract by default. A class implementing an interface must implement all methods declared in the interface.
Example
Here are some examples of Interface in C#:
- Example 1:
Output format: Sudo Placement GeeksforGeeks
- Example 2:
The output format is:
4. Conclusion
- Interfaces are used to ensure loose coupling.
- They are used to ensure abstraction.
- To ensure component-based programming.
- Ensuring multiple inheritance and abstraction.
- Interfaces serve as plug-ins and play a role as architecture in applications.
This article from Mytour introduces you to what Interface is in C#. Additionally, readers can explore more articles on Mytour to learn about classes and objects in C#.