C# Program to Demonstrate the IDumpable Interface
Posted by Superadmin on August 12 2022 05:47:54

C# Program to Demonstrate the IDumpable Interface

 

 

This C# Program Demonstrates IDumpable Interface.Here the Dumpable Interface is demonstrated with the use of Dump method.

 

Here is source code of the C# Program to Demonstrate IDumpable Interface. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

  1. /*
  2.  * C# Program to Demonstrate IDumpable Interface
  3.  */
  4. using System;
  5. interface IDumpable
  6. {
  7.     string Name { get; set; }
  8.     void Dump();
  9. }
  10.  
  11. class Fraction : IDumpable
  12. {
  13.     int z, n;
  14.     string name;
  15.  
  16.     public Fraction(int z, int n)
  17.     {
  18.         this.z = z; this.n = n;
  19.     }
  20.  
  21.     public string Name
  22.     {
  23.         get
  24.         {
  25.             return name;
  26.         }
  27.         set
  28.         {
  29.             name = value;
  30.         }
  31.     }
  32.  
  33.     public void Dump()
  34.     {
  35.         Console.WriteLine("Fraction : " + z + "/" + n);
  36.     }
  37. }
  38.  
  39. class Person : IDumpable
  40. {
  41.     string name;
  42.     public string address;
  43.     public int phone;
  44.  
  45.     public Person(string name, string address, int phone)
  46.     {
  47.         this.name = name; this.address = address; this.phone = phone;
  48.     }
  49.  
  50.     public string Name
  51.     {
  52.         get { return name; }
  53.         set { name = value; }
  54.     }
  55.  
  56.     public void Dump()
  57.     {
  58.         Console.WriteLine("Person Details : {0}, {1}, {2}", name, address, phone);
  59.     }
  60. }
  61.  
  62. class Test
  63. {
  64.  
  65.     static void Main(string[] arg)
  66.     {
  67.         IDumpable[] a = 
  68.         {
  69.             new Fraction(10,3),
  70.             new Fraction(9,4),
  71.             new Person("Tom", "INDIA", 99556677),
  72.             new Person("Jerry", "INDIA", 998979899),
  73.         };
  74.         a[0].Name = "f1";
  75.         a[1].Name = "f2";
  76.         foreach (IDumpable obj in a)
  77.         {
  78.             Console.Write(obj.Name + ": ");
  79.             obj.Dump();
  80.         }
  81.         Console.ReadLine();
  82.     }
  83.  
  84. }

Here is the output of the C# Program:

f1 : Fraction : 10/3
f2 : Fraction : 9/4
Tom : Person Details : Tom, INDIA, 99556677
Jerry : Person Details :Jerry, INDIA, 998979899