Use of Interface in C# – Why is it Required

Interfaces is not an unknown concept in any object oriented programming language. But its often confusing that why is it even required when one can simply create classes, initialize the instances and invoke the required method. Also, it seems to be a very important question for interviewers while hiring for experienced software developers. Lets discuss in detail what is the use of interface in C# and why do we require it.

It provides abstraction

Lets discuss a scenario for the same.

We have game class containing a list of players and a RunGame Method which runs the game by giving a chance to play for each player as below

    class Game
    {
        private List<Player> players { get; set; }


        public RPSGame(List<Player> _players)
        {
            players = _players;
        }

        public void RunGame()
        {
            players.ForEach(player =>
            {
                player.Play();
            });
        }
    }

The player class has all the functionality to play for any player.

    class Player
    {       
        public void Play()
        {
            // Code for playing functionality
        }
    }

So far so good. Suppose now we have to add a feature in which the user can play alone and others will be bot players. So now there can be 2 types of players, user-player and bot-player. One way of doing this is by creating 2 lists – UserPlayers and BotPlayers as below:

    class Game
    {
        private List<UserPlayer> userPlayers { get; set; }
        private List<BotPlayer> botPlayers { get; set; }

        public RPSGame(List<UserPlayer> _userPlayers, List<BotPlayer> _botPlayers)
        {
            userPlayers = _userPlayers;
            botPlayers = _botPlayers;
        }

        public void RunGame()
        {
           // invoke UserPlayer Play method and BotPlayer Play method alternatively to give each one a chance to play
        }
    }

UserPlayer and BotPlayer classes implementation as below:


    class UserPlayer
    {
        public void Play()
        {
            // Code for playing functionality of user
        }
    }



    class BotPlayer
    {
        public void Play()
        {
            // Code for playing functionality of Bot
        }
    }

The problem we identify in above code implementation:

There are 2 problems in the above implementation:

  • The Game class must not have the responsibility of managing the user players and bot players separately. For Game class it must not matter which player it is. For both types of players, the game must be same and hence their handling in game class. This is abstraction for the Players in game class, i.e. the Game must not worry about how any player plays the game. Hence, it must have a single list of players.

  • If their are new types of players in future, like, remote player, then we have to make changes to the Game class. Thus, this violates Single Responsibility Principle.

The Solution:

The above problems can be solved by using interfaces. We can create an interface IPlayer and implement it into 2 Concrete class (UserPlayer and BotPlayer). Now, the Game class will only accept a list of IPlayers and iterate over it.

    class Game
    {
        public List<IPlayer> players { get; set; }

        public Game(List<IPlayer> _players)
        {
            players = _players;
        }

        public void RunGame()
        {
            players.ForEach(player =>
            {
                player.Play();
            });
        }
    }

The Players implementation as below:

    public interface IPlayer
    {
        public void Play();
    }

    class UserPlayer : IPlayer
    {
        public void Play()
        {
          //Code for play 
        }
    }

    class BotPlayer : IPlayer
    {
        public void Play()
        {
           //Bot Playing Game
        }
    }

Now the Game class has nothing to worry about any bot player of user player. It is now the responsibility of the caller to initialize the IPlayer List and pass it on to the Game. The list may contain any kind of player.

Now, in future, if we introduce more types of players, there will be no change in the Game class since it is not Game Class’ responsibility.

The major use of interfaces in C# is that is also make code more manageable in applications as the code base increases with time.

Below is the link for above scenario implementation in an Rock-Scissor-Paper game implementation using interfaces.

https://github.com/akshay0905/RPSGame

Conclusion

We looked upon what is the use of interface in C# with real time example in application development. Please let us know your suggestions/queries in below comment box.

1 thought on “Use of Interface in C# – Why is it Required”

  1. Pingback: How to Implement IDisposable Interface in C# - Eastern Coder

Leave a Comment

Your email address will not be published.