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
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.
This C# Program to Use Delegate to Call 2 Methods within which First method Prints to Console and Second Method Prints to File.
Here it uses delegates to create two methods one to print to the console and other to file.
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(); } } }
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.
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