C# Program to Call Two Methods using Delegates

This is a C# Program to use delegate to call 2 methods within which first method prints to console and second method prints to file.

Problem Description

This C# Program to Use Delegate to Call 2 Methods within which First method Prints to Console and Second Method Prints to File.

Problem Solution

Here it uses delegates to create two methods one to print to the console and other to file.

Program/Source Code

Here is source code of the C# Program to Use Delegate to Call 2 Methods within which First method Prints to Console and Second Method Prints to File. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Use Delegate to Call 2 Methods within which First method 
 * Prints to Console and Second Method Prints to File
 */
using System;
using System.IO;
namespace Program
{
    class PrintString
    {
        static FileStream fs;
        static StreamWriter sw;
        public delegate void printString(string s);
        public static void Screen(string str)
        {
            Console.WriteLine("The String is: {0}", str);
        }
        public static void File(string s)
        {
            fs = new FileStream("c:\\sri\\Message.txt",
            FileMode.Append, FileAccess.Write);
            sw = new StreamWriter(fs);
            sw.WriteLine(s);
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        public static void sendString(printString ps)
        {
            ps("C# Program to Use Delegate to Call 2 Methods within which First " + 
               "method Prints to Console and Second Method Prints to File");
        }
        static void Main(string[] args)
        {
            printString ps1 = new printString(Screen);
            printString ps2 = new printString(File);
            sendString(ps1);
            sendString(ps2);
            Console.ReadKey();
        }
    }
}
Program Explanation

This C# program is used to call 2 methods within which first method prints to console and second method prints to file. Create an object for printString class using ‘ps1’ and ‘ps2’ variables respectively and pass the value of ‘screen’ and ‘file’ variables as an argument. Then pass the value of ‘ps1’ and ‘ps2’ variable values as an argument to sendString() function. Here it uses delegates to create two methods one to print to the console and other to file.

advertisement
advertisement
Runtime Test Cases
 
The String is : C# Program to Use Delegate to Call 2 Methods within which 
First method Prints to Console and Second Method Prints to File

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.