---
title: "Events With Delegate"  
description: "using System; using System.Collections.Generic;  using System.Linq;  using System.Text;  using System.IO;    namespace Events    // Event Publisher"  
author: "Sanjay Singh"  
published: 2011-12-23  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/851/events-with-delegate  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Events With Delegate

```
using System;
using System.Collections.Generic;using System.Linq;using System.Text;using System.IO; namespace Events{    // Event Publisher        public class DelegateEvent        {             public delegate void AttendanceLogHandler(string Message);              // Define an Event based on the above Delegate            public event AttendanceLogHandler EventLog;              public void LogProcess()            {                string Reason = null;         
            Console.WriteLine("Enter your name");                string UserName = Console.ReadLine();                DateTime t = DateTime.Now;                int hr = t.Hour;                int m = t.Minute;                if (!(hr >= 9 && hr < 10 || (hr == 10 && m == 0)))                {                    Console.WriteLine("Enter the reason for not coming within the valid time:");                    Reason = Console.ReadLine();                }                 OnEventLog("Logging the info :" + UserName);                if (hr >= 9 && hr < 10 || (hr == 10 && m == 0))                    if (hr < 10)                        OnEventLog(" Logged in at " + hr.ToString() + ":0" + m.ToString() + " Within Time");                    else                       OnEventLog(" Logged in at " + hr.ToString() + ":" + m.ToString() + " Within Time");                else                    if (hr > 10)                        OnEventLog(" Logged in at " + hr.ToString() + ":0" + m.ToString() + " not Within Time because " + Reason);                    else                        OnEventLog(" Logged in at " + hr.ToString() + ":" + m.ToString() + " not Within Time because " + Reason);                OnEventLog(" ");            }             // By Default, create an OnZZZZ Method, to call the Event            protected void OnEventLog(string Message)            {                if (EventLog != null)                {                    EventLog(Message);                }            }        }         // The AttendanceLogger class merely encapsulates the file I/O        public class AttendanceLogger        {            FileStream FileStr;            StreamWriter StreamWrtr;             // Constructor            public AttendanceLogger()           {                FileStr = new FileStream("process4.txt", FileMode.Append, FileAccess.Write);                StreamWrtr = new StreamWriter(FileStr);            }             // Member Function which is used in the Delegate            public void Logger(string LogInfo)            {                StreamWrtr.WriteLine(LogInfo);            }             public void Close()            {                StreamWrtr.Close();                FileStr.Close();            }        }         //Subscriber of the Event        public class RecordAttendance        {            static void Logger(string LogInfo)            {                Console.WriteLine(LogInfo);            }             static void Main(string[] args)            {                 AttendanceLogger FileLog = new AttendanceLogger();                DelegateEvent DEvent = new DelegateEvent();                 // Subscribe the Functions Logger and FileLog.Logger                DEvent.EventLog += new DelegateEvent.AttendanceLogHandler(Logger);                DEvent.EventLog += new DelegateEvent.AttendanceLogHandler(FileLog.Logger);                 // The Event will now be triggered in the LogProcess() Method                DEvent.LogProcess();                Console.ReadLine();                FileLog.Close();            }        }    } 
```

Here is Output……

![Events With Delegate](https://www.mindstick.com/mindstickarticle/554f114d-b79b-4728-8423-86caa269c31b/images/59b78e68-8576-4c6c-9733-7bc4b64aaf47.png)

\

---

Original Source: https://www.mindstick.com/articles/851/events-with-delegate

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
