Sunday, 3 December 2017

C# Program to Implement Delegates


Code:

using System;
using System.IO;
delegate void delegatewriter(string message);
class delgwriter
{
    StreamWriter w;
    public delgwriter(string path)
    {
        w = File.CreateText(path);
    }
    public void display(string msg)
    {
        w.WriteLine(msg);
    }
    public void Flush()
    {
        w.Flush();
    }
    public void Close()
    {
        w.Close();
    }
}
class Test
{
    static delegatewriter delgwri;
    static void display(string s)
    {
        Console.WriteLine(s);
    }
    static void Main(string[] arg)
    {
        delgwriter x = new delgwriter("log.txt");
        delgwri += new delegatewriter(display);
        delgwri += new delegatewriter(x.display);
        delgwri("C");
        delgwri("C++");
        delgwri("Java");
        x.Flush();
        x.Close();
        Console.Read();
    }
}


Output:

C
C++
Java



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...