Idumpable Interface In C#

In this guide, we will explore the process of incorporating the IDumpable interface in C#. The IDumpable interface is straightforward, comprising a Dump method along with publicly accessible properties. Any classes seeking to adhere to the IDumpable interface are required to define the Dump method while also having the option to leverage the interface's public properties to regulate the execution of their code.

Purpose of IDumpable:

The main objective of the IDumpable Interface is to enforce agreements on objects that employ the Dump method. Through the utilization of the Dump method, one can capture and comprehend the internal state of the object. Subsequently, this data is converted into a string output. This functionality proves to be particularly valuable for tasks such as debugging, logging, or scenarios requiring insight into an object's content without delving into its internal composition.

Real-Time Example:

Let's consider a practical scenario to demonstrate the IDumpable Interface in C#. Imagine we're in a car and need to send a command to specify the fuel type for the car to utilize - petrol, diesel, CNG, or electricity. In this case, we employ the Dump method to ascertain the appropriate command for driving the car according to the specified fuel type. These commands are accessed via public interface properties dynamically. Initially, establish different enumeration types for the commands, followed by the DriveCommand enumeration that outlines the four distinct commands for operating the car.

Syntax:

It has the following syntax:

Example

Internal enum DriveCmd{
Petrol,
Diesel,
Cng
}

The subsequent task is to generate an IDumpable interface that consists of a Dump function and an associated public property of Command type, named with the DriveCmd enumeration.

Example

Public interface IDumpable{
  DriveCmd Cmd{
  Get;
   Set;
   }
   Void Dump();
}

Example:

Let's consider a scenario to demonstrate the IDumpable Interface in C#.

Example

using System;

namespace Sample {
internal class Structure{
	internal enum DriveCmd {
		Using_PETROL,
		Using_DIESEL,
		Using_CNG,
		Using_ELECTRIC
	}

	public interface IDumpable {
		DriveCmd Cmd
		{
			get;
			set;
		}
		void Dump();
	}

	public class BMW : IDumpable {
		private DriveCmd _cmd;
		private int _velocity;

		public DriveCmd Cmd
		{
			get { return _cmd; }
			set { _cmd = value; }
		}

		public BMW(int Velocity) { this._velocity = Velocity; }

		public void Dump()
		{
			Console.WriteLine("I AM DRIVING THE BMW");
			Console.WriteLine("Cmd : " + _cmd);

			if (_cmd == DriveCmd.Using_PETROL) {
				Console.WriteLine(
					"BMW CURRENTLY RUNS WITH " + _cmd
					+ " AT A SPEED OF " + _velocity.ToString()
					+ " KM/HR"
					+ "\n");
			}
			else {
					Console.WriteLine("BMW IS NOT SUITABLE TO DRIVE ON " + _cmd + "\n");
			}
		}
	}

	public class TRACTOR : IDumpable {
		private DriveCmd _cmd;
		private int _velocity;

		public DriveCmd Cmd
		{
			get { return _cmd; }
			set { _cmd = value; }
		}

		public TRACTOR(int Velocity) { this._velocity = Velocity; }

		public void Dump()
		{
			Console.WriteLine("I AM DRIVING THE TRACTOR");
			Console.WriteLine("Cmd : " + _cmd);

			if (_cmd == DriveCmd.Using_DIESEL) {
				Console.WriteLine(
					"TRACTOR CURRENTLY RUNS WITH " + _cmd
					+ " WITH A SPEED OF " + _velocity.ToString()
					+ " KM/HR"
					+ "\n");
			}
			else {
				Console.WriteLine(
					"TRACTOR IS NOT SUITABLE TO DRIVE ON "
					+ _cmd + "\n");
			}
		}
	}

	public class TUCSON: IDumpable {
		private DriveCmd _cmd;
		private int _velocity;

		public DriveCmd Cmd
		{
			get { return _cmd; }
			set { _cmd = value; }
		}

		public TUCSON(int Velocity) { this._velocity = Velocity; }

		public void Dump()
		{
			Console.WriteLine("I AM DRIVING THE TUCSON");
			Console.WriteLine("Cmd : " + _cmd);

			if (_cmd == DriveCmd.Using_PETROL
				|| _cmd == DriveCmd.Using_ELECTRIC) {
				Console.WriteLine(
					"TUCSON CURRENTLY RUNS WITH " + _cmd
					+ " WITH SPEED OF " + _velocity.ToString()
					+ " KM/HR"
					+ "\n");
			}
			else {
					Console.WriteLine("TUCSON IS NOT COMPATIBLE TO DRIVE	ON " + _cmd + "\n");
			}
		}
	}

	public class WAGONR : IDumpable {
		private DriveCmd _cmd;
		private int _velocity;

		public DriveCmd Cmd
		{
			get { return _cmd; }
			set { _cmd = value; }
		}

		public WAGONR(int Velocity) { this._velocity= Velocity; }

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

			if (_cmd == DriveCmd.Using_PETROL
				|| _cmd == DriveCmd.Using_CNG) {
				Console.WriteLine(
					"WAGONR CURRENTLY RUNS WITH " + _cmd
					+ " WITH SPEED OF " + _velocity.ToString()
					+ " KM/HR"
					+ "\n");
			}
			else {
					
                                    Console.WriteLine("WAGONR IS NOT SUITABLE TO DRIVE ON " + _cmd + "\n");
			}
		}
	}

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

		Car.Cmd = DriveCmd.Using_PETROL;
		Car.Dump();

		Car.Cmd = DriveCmd.Using_DIESEL;
		Car.Dump();

		Car = new TRACTOR(100);

		Car.Cmd = DriveCmd.Using_DIESEL;
		Car.Dump();

		Car = new TUCSON(80);

		Car.Cmd = DriveCmd.Using_PETROL;
		Car.Dump();

		Car.Cmd = DriveCmd.Using_ELECTRIC;
		Car.Dump();

		Car = new WAGONR(50);

		Car.Cmd = DriveCmd.Using_CNG;
		Car.Dump();

		Console.Read();
	}
}
}

Output:

Output

I AM DRIVING THE BMW
Cmd : Using_PETROL
BMW CURRENTLY RUNS WITH Using_PETROL AT A SPEED OF 90 KM/HR

I AM DRIVING THE BMW
Cmd : Using_DIESEL
BMW IS NOT SUITABLE TO DRIVE ON Using_DIESEL

I AM DRIVING THE TRACTOR
Cmd : Using_DIESEL
TRACTOR CURRENTLY RUNS WITH Using_DIESEL WITH A SPEED OF 100 KM/HR

I AM DRIVING THE TUCSON
Cmd : Using_PETROLTUCSON CURRENTLY RUNS WITH Using_PETROL WITH SPEED OF 80 KM/HR

I AM DRIVING The TUCSON
Cmd : Using_ELECTRIC
TUCSON CURRENTLY RUNS WITH Using_ELECTRIC WITH SPEED OF 80 KM/HR

I AM DRIVING WAGONR
Cmd : Using_CNG
WAGONR CURRENTLY RUNS WITH Using_CNG WITH SPEED OF 50 KM/HR

Input Required

This code uses input(). Please provide values below: