class SimpleDelegate
{
public delegate void LogHandler(string message);
public void Process(LogHandler logHandler)
{
if (logHandler != null)
{
Console.WriteLine("Process begin");
}
if (logHandler != null)
{
Console.WriteLine("Process end");
}
}
}
class FileLogger
{
FileStream fileStream;
StreamWriter writer;
public FileLogger(string fileName)
{
fileStream = new FileStream(fileName, FileMode.Create);
writer = new StreamWriter(fileStream);
}
public void Logger(string s)
{
writer.WriteLine(s);
}
public void Close()
{
writer.Close();
fileStream.Close();
}
}
class Program
{
static void Main(string[] args)
{
SimpleDelegate cp = new SimpleDelegate();
FileLogger fl = new FileLogger(@"C:\TEMP\MyLog.log");
SimpleDelegate.LogHandler handler = null;
handler += new SimpleDelegate.LogHandler(Logger);
handler += new SimpleDelegate.LogHandler(fl.Logger);
cp.Process(handler);
fl.Close();
}
static void Logger(string s)
{
Console.WriteLine("writing s " + s);
}
}
On executing I get following output: Process begin Process end Press any key to continue . . .
Actually it should be, Process begin Process end Process begin Process end Press any key to continue . . .
It's week, I am not able to figure out the mistake :(