Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Dániel Szabó

Allowed Interface Members in C#

Dániel Szabó

  • Mar 5, 2020
  • 6 Min read
  • Mar 5, 2020
  • 6 Min read
Languages Frameworks and Tools


In C#, the interface revolves around two main concepts: contract and inheritance. An interface is basically a syntactical contract that defines a guideline that classes inheriting a specific interface should follow. In simpler terms, the interface defines the what and the class deriving from it defines the how regarding the contract. Basically, interfaces provide a very similar functionality to abstract classes.

In this guide, we will walk through each part of this contract and see how they glue together to provide the functionality we all love and use today.


We have several different interface members:

  1. Properties
  2. Methods
  3. Events
  4. Indexers

These are like skeletons, and the deriving classes need to take care of filling them up with functionality.

In this demonstration we'll declare an interface called ServerInterface.

1public interface ServerInterface
2    {
3        string name { get; set; }
4        string status { get; set; }
5        void restart();
6        void getStatus();
7        void start();
8        void stop();
9        event EventHandler statusChanged;
10    }

This interface declares two properties, name and status. It also declares four methods, restart,start,stop and getStatus. We have an event handler called statusChanged.

In order to enforce our contract, the class Server needs to inherit from this interface.

1public class Server : ServerInterface
2    {
4    }

Due to the nature of the contract, we need to define the inherited members in our class. If we do not define them, we will get the following error from our compiler:

1'Server' does not implement interface member ''	
2'Server' does not implement interface member 'ServerInterface.type'	
3'Server' does not implement interface member 'ServerInterface.restart()'
4'Server' does not implement interface member 'ServerInterface.status()'
5'Server' does not implement interface member 'ServerInterface.statusChanged'

This explicitly tells us what we need to do. Let's look at the full-blown app and dissect it.

1using System;
3namespace Pluralsight
5    public interface ServerInterface
6    {
7        string name { get; set; }
8        string status { get; set; }
9        void restart();
10        void stop();
11        void start();
12        void getStatus();
13        event EventHandler statusChanged;
14    }
15    public class MyEventArgs : EventArgs
16    {
17    }
18    public class Server : ServerInterface
19    {
20        public string name { get; set; }
21        public string status { get; set; }
22        public event EventHandler statusChanged;
23        public Server(string Name)
24        {
25            name = Name;
26            status = "off";
27        }
28        public void getStatus()
29        {
30            Console.WriteLine($"THe server: {} is {this.status}");
31        }
32        public void start()
33        {
34            if(this.status == "off") { Console.WriteLine($"Starting server: {}"); this.status = "on"; }
35            else if(this.status == "on") { Console.WriteLine($"THe server: {} is already started!"); }
36            else { Console.WriteLine($"The server is in an unknown state!"); }
37        }
38        public void stop()
39        {
40            if (this.status == "off") { Console.WriteLine($"The server: {} is already stopped!"); }
41            else if (this.status == "on") { Console.WriteLine($"Stopping server: {}"); this.status = "off"; }
42            else { Console.WriteLine($"The server is in an unknown state!"); }
43        }
44        public void restart() {
45            Console.WriteLine($"Event for restarting the server caught: {}");
46        }
47        protected virtual void restart(MyEventArgs e)
48        {
49            statusChanged?.Invoke(this, e);
50        }
52    }
53    public class Interfacing
54    {
56        public static void Main()
57        {
58            Server a = new Server("Domain Controller");
59            a.start();
60            a.stop();
61            a.restart();
63            Console.ReadKey();            
64        }
65    }

The output produced is as follows:

1Starting server: Domain Controller
2Stopping server: Domain Controller
3Event for restarting the server caught: Domain Controller

All that needs to be done is to implement our part of the contract. We defined the specific methods, properties, and event handler. In the Main() function, our class inherited from the interface comes alone. We see the instance initialized, and then call the appropriate methods to perform their duties. The associated event with the restart action is also fired.

Interface vs. Abstract Class

While the concepts of an interface and an abstract class revolve around the same idea, it is important to lay out the facts and decide which one to use in which situation. While the interface cannot be instantiated, the abstract class can. Interfaces are way slower than abstract classes because of the lookup time for the appropriate signatures. Interfaces cannot have access specifiers by default, while abstract classes can. Interfaces can only have abstract methods, while abstract classes can have full implementation with non-abstract methods.


In this guide, we have learned how interface members can be implemented and how to use them to our own advantage in a live example. We have also learned to implement different members. We also looked at the difference between abstract classes and interfaces, and now we can decide which one to use in specific situations. I hope this guide has been informative to you and I would like to thank you for reading it.