Home >Backend Development >C#.Net Tutorial >C# Interface
Interface, in C#, is a keyword, which holds a group of abstract methods and properties, which are to be implemented or used by an abstract or non-abstract class. Defining the methods are properties inside an interface which makes them public and abstract by default.
In simplest terms, An Interface is like a Contract, where every member or component included in the body has to follow the contract, it defines what must be done. The interface does not contain any fields and is always defined by the use of the keyword “interface”.
Syntax:
Syntax starts with the interface keyword followed by the name for the interface and then the body.
interface <name_for_interface> { //abstract methods //abstract properties. }
As you can see, we have our standard syntax for Interface in C#, which begins with the ‘interface’ keyword then the name for interface and then abstract methods and properties inside the body. In C#, multiple interfaces can be implemented and used, inside a class or a struct. These interfaces can hold various methods, indexers, properties and also events as members.
Basically we have understood that there is no specific functionality inside an interface, if it is so, then why do we need Interface?
When to use Interface?
public class MyClass : IMyInterface { public void Method1() { // Method implementation }public string Property1 { get; set; } public event EventHandler Event1; }
Now that we have understood what interface is and its need. Let’s demonstrate a simple example of C# code with interface implementation.
Program implements Interface and prints a simple statement.
Code:
using System; namespace MyApplication { interface SampleInterface { void InterfaceMethod(); } class Int_Example : SampleInterface { public void InterfaceMethod() { Console.WriteLine("\nThis is simple example of Interface in C#."); } } class Program { static void Main(string[] args) { Int_Example myInterface = new Int_Example(); myInterface.InterfaceMethod(); Console.Read(); } } }
Code Interpretation: Starting with the use and namespace, a basic interface is generated as a SampleInterface that has a single method in its body. This method inside an interface does not have any particular body. Then we have our new class which is to implement the interface we created. Created with, class keyword followed by the class name and then implementing the interface with colon symbol followed by the interface name. Inside our class Int_Example, we have our earlier created interface method, which was bodiless by then, now we have added the simple print statement, which says, ” This is a simple example of Interface in C#.”
Then begins our mail class, namely Program, with the static void main statement. Inside our main class, we have created a new object for our Int_Example class which inherits interface. The new object is created and to the next line, our method created earlier is called up. Finally, our newly created object will call the earlier created method and the body inside that method will be executed here. With Console.Read(); the program will wait for user input before exiting.
Output:
Upon successful compilation and execution, the program must simply print the statement: “This is a simple example of Interface in C#.”
Arithmetic operations using the interface.
Code:
using System; namespace arth_interface { public interface SampleInterface { void sam_add(int a, int b); void sam_sub(int a, int b); void display(); } class interface_class : SampleInterface { int x, y; public void sam_add(int a, int b) { int m, n; m = a; n = b; x = m + n; } public void sam_sub(int a, int b) { int m, n; m = a; n = b; y = a - b; } public void display() { Console.WriteLine("Added Value is:" + x); Console.WriteLine("Subtracted value is:" + y); } } class arth_interface { static void Main(string[] args) { interface_class obj_interface_class = new interface_class(); int fnumber, snumber; Console.WriteLine("Please Enter 1st Number to perform Addition and Subtraction:"); fnumber = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Now 2nd Number to perform Addition and Subtraction:"); snumber = Convert.ToInt16(Console.ReadLine()); obj_interface_class.sam_add(fnumber, snumber); obj_interface_class.sam_sub(fnumber, snumber); obj_interface_class.display(); Console.ReadKey(); } } }
Code Interpretation: Similar to our first example, we have used and namespace statements, followed by the interface and its body with methods. We have two basic methods for addition and subtraction with void as return type, two integers inside every method, respectively. Next, we have our class which implements our interface.
We’ve declared two integers and then we have our first method to calculate addition. Here is the operation that needs to be done for addition and the same is for the subtraction. Then we have our display method, which consists of two print statements, printing addition and subtraction values of the numbers passed.
Finally, we have our class with the main method, where we initially created an object for our interface. Then the program prints “Please Enter the 1st Number to perform Addition and Subtraction:”, where the user inputs a first number and the later second number, for the purpose of calculations. With the object created earlier, the program calls the add and sub-methods from the interface and the same operations are done. At last, we have our display method, which displays our results as defined in the display method and ReadKey(); method holds up our program until any key is pressed.
Output:
Below are some of the advantages given.
We have understood what Interface in C# is. The proper syntax for an interface along with an explanation. To wrap it up, Interfaces in C# are a way to fill the emptiness of multiple inheritances in the language. Later we learned why do we actually need the interface in C# followed by the examples to demonstrate the understanding of the interfaces. The first example was to demonstrate simple use of interface while with the second example we implemented arithmetic operations, followed by Code Interpretation and output screenshot.
The above is the detailed content of C# Interface. For more information, please follow other related articles on the PHP Chinese website!