Sunday, 3 December 2017

C# Program to Demonstrate IDumpable Interface


Code:

using System;
interface IDumpable
{
    string Name { get; set; }
    void Dump();
}

class Fraction : IDumpable
{
    int z, n;
    string name;

    public Fraction(int z, int n)
    {
        this.z = z; this.n = n;
    }

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    public void Dump()
    {
        Console.WriteLine("Fraction : " + z + "/" + n);
    }
}

class Person : IDumpable
{
    string name;
    public string address;
    public int phone;

    public Person(string name, string address, int phone)
    {
        this.name = name; this.address = address; this.phone = phone;
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public void Dump()
    {
        Console.WriteLine("Person Details : {0}, {1}, {2}", name, address, phone);
    }
}

class Test
{

    static void Main(string[] arg)
    {
        IDumpable[] a = 
        {
            new Fraction(10,3),
            new Fraction(9,4),
            new Person("Tom", "INDIA", 99556677),
            new Person("Jerry", "INDIA", 998979899),
        };
        a[0].Name = "f1";
        a[1].Name = "f2";
        foreach (IDumpable obj in a)
        {
            Console.Write(obj.Name + ": ");
            obj.Dump();
        }
        Console.ReadLine();
    }

}


Output:

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


More C# Programs:





















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...