Home » C# Tutorial » What is C# Interface and How to Use It?

What is C# Interface and How to Use It?

What makes C# different from many other programming languages and sets it apart is the fact that it is one of the most versatile coding scripts out there. It proves to be a lucrative coding language for beginners and experts alike as it provides many different methods to perform similar tasks. This gives the coder or developer freedom of choice over which method they may be comfortable using. In today’s article, we will be looking at the basics of a concept similar to that of a class: C# Interface.

What is C# Interface?

The interface in C# is an excellent feature that allows the programmer to write a much cleaner, more reusable code, by defining a set of methods that we can then implement in various classes. It is one of the main concepts in C# for writing object-oriented code. Essentially, they allow you to define properties and functions, but without any implementation. 

The interface acts as a contract. This is because when a class implements a specified interface, it must implement those functions that have been defined by the interface in C#. This allows you to have several varied classes that can be used in the same way using a common interface.

Interfaces cannot contain fields. If a struct or a class inherits from a particular interface, it must provide implementation for all of the interface members. If not, we will get a compile-time error that will not let our program execute. As far as inheritance goes, a class or struct can inherit from more than one interface at the same time, whereas, a class cannot inherit from more than one class at the same time. Interfaces can also inherit from other interfaces. A class that inherits this interface must provide implementation for all of the interface members in the complete interface inheritance chain.

It is impossible to create an instance of an interface, however, an interface reference variable can point to a derived class object.

How to use C# Interface?

We create interfaces using the ‘interface’ keyword. Just like classes, interfaces also contain properties, methods, delegates or events, but only declarations and no implementations. Interfaces are extensively used in live projects. Before the value that you attribute to the interface, i.e., the name that you may want to associate with it, you will need to affix a capital ‘I’ to it. For instance, if we were making an interface for students, the name of the desired interface would need to be ‘IStudents’. This is a common naming convention for Interfaces in C#.

As mentioned earlier, unlike classes or structs, interfaces can only have declarations and not implementations. For instance, if the following were to be your code, you would get the resulting compile-time error.

using System;
using System.Collections;
using System.Collections.Generic;
 
interface IStudent {
void Print(){
  Console.WriteLine("Hello World!");
}
}
public class Program{
  public static void Main()
  {
 
  }
}

OUTPUT:

IStudent.Print()’s interface members cannot have a definition.

One more factor to remember is that since interface members are public in nature by default, adding a public modifier before the member will also result in a compile-time error, as demonstrated in the example below:

using System;
using System.Collections;
using System.Collections.Generic;
 
interface IStudent {
public void Print();
}
public class Program{
  public static void Main()
  {
 
  }
}

OUTPUT:

The modifier ‘public’ is not valid for this item.

Similarly, C# interfaces cannot contain fields either, as this will also result in a compile-time error. The only time the members of an interface can be implemented is if a class or a struct inherits that particular interface.

C# Interface Examples:

Example 1:

In the following example, we will show you how to use interfaces in tandem with classes in order to derive a particular function and its result. In this example, we will be considering a class of students with a student named Alex.

using System;
using System.Collections;
using System.Collections.Generic;
 
interface IStudent {
void studentGrade(); // note that an interface method cannot have a body
}
// 'Alex' implements the IStudent interface
class Alex : IStudent
{
  public void studentGrade()
  {
    // The contents of studentGrade is given here
    Console.WriteLine("Alex's grade is B-");
  }
} 
class Program
{
  static void Main(string[] args)
  {
    Alex stdAlex = new Alex(); 
    // This is to create a student object with the name 'Alex'
    stdAlex.studentGrade();
  }
  }

OUTPUT:

Alex’s grade is B-

Even though classes are abstract in nature, unlike abstract classes, it is impossible to use interfaces to create objects. As seen in the example above, the interface IStudent cannot be used to create an object in the Program class.

Interfaces are mostly used to maximize security as it effectively hides certain details and information to only display the important data of any interface. Finally, let us look at how we can implement multiple interfaces.

Example 2:

using System;
using System.Collections;
using System.Collections.Generic;
 
interface IInterfaceA
{
  void IntMethod1();
  // this will act as the interface method
}
interface IInterfaceB
{
  void IntMethod2();
  // this will act as an additional interface method
}
// in order to implement multiple interfaces, follow the next steps
class MultiInt : IInterfaceA, IInterfaceB
{
  public void IntMethod1()
  {
    Console.WriteLine("This is the content of the first interface.");
  }
  public void IntMethod2()
  {
    Console.WriteLine("This is the content of the second interface.");
  }
}
class Program
{
  static void Main(string[] args)
  {
    MultiInt myObj = new MultiInt();
    myObj.IntMethod1();
    myObj.IntMethod2();
  }
}

OUTPUT:

This is the content of the first interface.
This is the content of the second interface.

And just like that, now you have a broad understanding of how you can use C# interfaces to enhance your coding experience and add a dynamic flair to your programs. It is used by developers in live projects, such as games, app-testing, and A/B testing scenarios. For a beginner, you may not need to use interfaces in C# as often but they are an efficient and useful tool that comes in handy as you increase your coding expertise and move on to more complex programs.

Do you know?
1. C# Enum
2. C# Dictionary
3. C# Switch Case
4. C# Substring
5. C# Foreach
6. C# Struct
7. C# Interface

Pin It on Pinterest