Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Implementing IDumpable Interface in C#

1.40/5 (11 votes)
24 Aug 2022CPOL2 min read 6.9K   37  
How to implement IDumpable Interface
The IDumpable interface is just a simple interface which has a Dump() method and a public property. Every class that wishes to implement the IDumpable interface has to implement the Dump() method and can make use of public property to manage the execution of the code.

Introduction

In this post, I am going to talk about how to implement IDumpable interface through C#. I will be using the latest .NET framework and Visual Studio 2022 to write the code and compile.

Let’s get started!!!

The IDumpable interface is just a simple interface which has a Dump() method and a Public Property. Every class that wishes to implement the IDumpable interface has to implement the Dump() method and can make use of Public Property to manage the execution of the code.

Let’s understand more in detail with a real time example.

Let’s say you are driving a car and you would like to give a command in order to know whether a car needs to drive on Petrol/Diesel/CNG/Electricity. Here, we will use the Dump() method to decide based on what command we would like to drive a car. Command will be given at run time through public interface property. Let’s understand by writing a code.

Implementation

First, I will be defining Enums for different commands. DriveCommand enum will have four commands defined on which my car will drive.

C#
internal enum DriveCommand
{ 
     PETROL, 
     DIESEL, 
     CNG, 
     ELECTRIC 
}

Now let’s define our IDumpable interface which will consist of the Dump() method and public property called Command of type DriveCommand enum.

C#
public interface IDumpable
{
    DriveCommand Command 
    { 
       get; 
       set; 
    }
    void Dump();
}

Now, let’s create our Car classes and inherit the IDumpable interface.

C#
public class BMW : IDumpable
{
    private DriveCommand _command;
    private int _speed;

    public DriveCommand Command
    {
        get { return _command; }
        set { _command = value; }
    }

    public BMW(int Speed)
    {
        this._speed = Speed;
    }

    public void Dump()
    {
        Console.WriteLine("I AM DRIVING BMW");
        Console.WriteLine("Command : " + _command);

        if (_command == DriveCommand.PETROL)
        {
            Console.WriteLine("BMW IS NOW DRIVING ON " + _command + 
            " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
        }
        else
        {
             Console.WriteLine("BMW IS NOT COMPATIBLE TO DRIVE ON " + _command + "\n");
        }
    }
}

The BMW class consists of two private variables.

  1. _command: This is going to be a command given by the user to class BMW.
  2. _speed: Which is assigned via a parameterized constructor. This is going to be the speed at which Car will drive.

The Dump() method consists of our main logic to decide how the car will drive. As BMW is only driving on Petrol, it will check whether the given command is Petrol or not. If the command is Petrol, the method will prompt a message that the car is driving on Petrol with a given speed. But if the command is not petrol, then the method will prompt a message that Car is not compatible to drive on given command.

Let’s add a few more car classes with different driving modes.

C#
public class TRUCK : IDumpable
{
      private DriveCommand _command;
      private int _speed;

      public DriveCommand Command
      {
          get { return _command; }
          set { _command = value; }
      }

      public TRUCK(int Speed)
      {
          this._speed = Speed;
      }

      public void Dump()
      {
          Console.WriteLine("I AM DRIVING TRUCK");
          Console.WriteLine("Command : " + _command);

          if (_command == DriveCommand.DIESEL)
          {
              Console.WriteLine("TRUCK IS NOW DRIVING ON " + 
              _command + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
          }
          else
          {
              Console.WriteLine("TRUCK IS NOT COMPATIBLE TO DRIVE ON " + 
                                 _command + "\n");
          }
      }
 }

public class TUCSON : IDumpable
{
      private DriveCommand _command;
      private int _speed;

      public DriveCommand Command
      {
          get { return _command; }
          set { _command = value; }
      }

      public TUCSON(int Speed)
      {
          this._speed = Speed;
      }

      public void Dump()
      {
          Console.WriteLine("I AM DRIVING TUCSON");
          Console.WriteLine("Command : " + _command);

          if (_command == DriveCommand.PETROL || _command == DriveCommand.ELECTRIC)
          {
              Console.WriteLine("TUCSON IS NOW DRIVING ON " + 
              _command + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
          }
          else
          {
              Console.WriteLine("TUCSON IS NOT COMPATIBLE TO DRIVE ON " + 
                                 _command + "\n");
          }
      }
}

public class WAGONR : IDumpable
{
      private DriveCommand _command;
      private int _speed;

      public DriveCommand Command
      {
          get { return _command; }
          set { _command = value; }
      }

      public WAGONR(int Speed)
      {
          this._speed = Speed;
      }

      public void Dump()
      {
          Console.WriteLine("I AM DRIVING WAGONR");
          Console.WriteLine("Command : " + _command);

          if (_command == DriveCommand.PETROL || _command == DriveCommand.CNG)
          {
              Console.WriteLine("WAGONR IS NOW DRIVING ON " + 
              _command + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
          }
          else
          {
              Console.WriteLine("WAGONR IS NOT COMPATIBLE TO DRIVE ON " + 
                                 _command + "\n");
          }
      }
}

Let’s summarize all our classes and on what command they can run.

  • Class BMW: Petrol
  • Class Truck: Diesel
  • Class Tucson: Petrol & Electric
  • Class WagonR: Petrol & CNG

Now, let’s call each Car’s Dump() method and see how it works. Before we call Dump() method, we will be passing DriveCommand to Public Property of IDumpable interface.

C#
static void Main(string[] args)
{
     IDumpable Car = new BMW(90);

     Car.Command = DriveCommand.PETROL;
     Car.Dump();

     Car.Command = DriveCommand.DIESEL;
     Car.Dump();

     Car = new TRUCK(100);

     Car.Command = DriveCommand.DIESEL;
     Car.Dump();

     Car = new TUCSON(80);

     Car.Command = DriveCommand.PETROL;
     Car.Dump();

     Car.Command = DriveCommand.ELECTRIC;
     Car.Dump();

     Car = new WAGONR(50);

     Car.Command = DriveCommand.CNG;
     Car.Dump();

     Console.Read();
}

Now when we run our code, we will get the following output.

Output

Image 1

Conclusion

That’s all. Our code worked perfectly! With the help of Public Property of Interface, I can pass the values to the class at run time or any time and depending up the value provided to Public Property, Dump() method will execute. I hope you have now understood how to implement IDumpable interface using C#. Here is the complete code for your reference.

C#
using System;

namespace Demo
{
    internal class Program
    {
        internal enum DriveCommand { PETROL, DIESEL, CNG, ELECTRIC }

        public interface IDumpable
        {
            DriveCommand Command { get; set; }
            void Dump();
        }

        public class BMW : IDumpable
        {
            private DriveCommand _command;
            private int _speed;

            public DriveCommand Command
            {
                get { return _command; }
                set { _command = value; }
            }

            public BMW(int Speed)
            {
                this._speed = Speed;
            }

            public void Dump()
            {
                Console.WriteLine("I AM DRIVING BMW");
                Console.WriteLine("Command : " + _command);

                if (_command == DriveCommand.PETROL)
                {
                    Console.WriteLine("BMW IS NOW DRIVING ON " + 
                    _command + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
                }
                else
                {
                    Console.WriteLine("BMW IS NOT COMPATIBLE TO DRIVE ON " + 
                                       _command + "\n");
                }
            }
        }

        public class TRUCK : IDumpable
        {
            private DriveCommand _command;
            private int _speed;

            public DriveCommand Command
            {
                get { return _command; }
                set { _command = value; }
            }

            public TRUCK(int Speed)
            {
                this._speed = Speed;
            }

            public void Dump()
            {
                Console.WriteLine("I AM DRIVING TRUCK");
                Console.WriteLine("Command : " + _command);

                if (_command == DriveCommand.DIESEL)
                {
                    Console.WriteLine("TRUCK IS NOW DRIVING ON " + 
                    _command + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
                }
                else
                {
                    Console.WriteLine("TRUCK IS NOT COMPATIBLE TO DRIVE ON " + 
                                       _command + "\n");
                }
            }
        }

        public class TUCSON : IDumpable
        {
            private DriveCommand _command;
            private int _speed;

            public DriveCommand Command
            {
                get { return _command; }
                set { _command = value; }
            }

            public TUCSON(int Speed)
            {
                this._speed = Speed;
            }

            public void Dump()
            {
                Console.WriteLine("I AM DRIVING TUCSON");
                Console.WriteLine("Command : " + _command);

                if (_command == DriveCommand.PETROL || 
                                _command == DriveCommand.ELECTRIC)
                {
                    Console.WriteLine("TUCSON IS NOW DRIVING ON " + 
                    _command + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
                }
                else
                {
                    Console.WriteLine("TUCSON IS NOT COMPATIBLE TO DRIVE ON " + 
                                       _command + "\n");
                }
            }
        }

        public class WAGONR : IDumpable
        {
            private DriveCommand _command;
            private int _speed;

            public DriveCommand Command
            {
                get { return _command; }
                set { _command = value; }
            }

            public WAGONR(int Speed)
            {
                this._speed = Speed;
            }

            public void Dump()
            {
                Console.WriteLine("I AM DRIVING WAGONR");
                Console.WriteLine("Command : " + _command);

                if (_command == DriveCommand.PETROL || _command == DriveCommand.CNG)
                {
                    Console.WriteLine("WAGONR IS NOW DRIVING ON " + 
                    _command + " WITH SPEED OF " + _speed.ToString() + " KM/HR" + "\n");
                }
                else
                {
                    Console.WriteLine("WAGONR IS NOT COMPATIBLE TO DRIVE ON " + 
                                       _command + "\n");
                }
            }
        }

        static void Main(string[] args)
        {
            IDumpable Car = new BMW(90);

            Car.Command = DriveCommand.PETROL;
            Car.Dump();

            Car.Command = DriveCommand.DIESEL;
            Car.Dump();

            Car = new TRUCK(100);

            Car.Command = DriveCommand.DIESEL;
            Car.Dump();

            Car = new TUCSON(80);

            Car.Command = DriveCommand.PETROL;
            Car.Dump();

            Car.Command = DriveCommand.ELECTRIC;
            Car.Dump();

            Car = new WAGONR(50);

            Car.Command = DriveCommand.CNG;
            Car.Dump();

            Console.Read();
        }
    }
}

History

  • 24th August, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)