What is Event?

What is Event?

Event:-The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers."

Example:-
            using System;
using System.IO;

namespace EventExample
{
      public class MyClass
    {
     public delegate void  LHandler(string message);
 
        // Define an Event based on the above Delegate
        public event LHandler Log;
  
        // Instead of having the Process() function take a delegate
        // as a parameter, we've declared a Log event. Call the Event,
                public void Process()
        {
            OnLog("Process() begin");
            OnLog("Process() end");
        }
 
           protected void OnLog(string message)
        {
            if (Log != null)
            {
                Log(message);
            }
        }
    }
 
    // The FLog class merely encapsulates the file I/O
    public class FLog
    {
        FileStream fileStream;
        StreamWriter streamWriter;
 
        // Constructor
        public FLog(string filename)
        {
            fileStream = new FileStream(filename, FileMode.Create);
            streamWriter = new StreamWriter(fileStream);
        }
 
        // Member Function which is used in the Delegate
        public void Logger(string s)
        {
            streamWriter.WriteLine(s);
        }
 
        public void Close()
        {
            streamWriter.Close();
            fileStream.Close();
        }
    }
 
       public class TestApplication
    {
        static void Logger(string s)
        {
            Console.WriteLine(s);
        }
 
        static void Main(string[] args)
        {
            FLog fl = new FLog("process.log");
            MyClass myClass = new MyClass();
 
            // Subscribe the Functions Logger and fl.Logger
            myClass.Log += new MyClass.LHandler(Logger);
            myClass.Log += new MyClass.LHandler(fl.Logger);

            // The Event will now be triggered in the Process() Method
            myClass.Process();
 
            fl.Close();
        }
    }
} 
Compile an test: 
# csc EventExample.cs
# EventExapmle.exe
Process() begin
Process() end
# cat process.log
Process() begin
Process() end 




An event is a placeholder for code that is executed when the event is triggered, or fired. Events are fired by a user action, program code, or by the system.




    The following important conventions are used with events:

        * Event Handlers in the .NET Framework return void and take two parameters.
        * The first paramter is the source of the event; that is the publishing object.
        * The second parameter is an object derived from EventArgs.
        * Events are properties of the class publishing the event.
        * The keyword event controls how the event property is accessed by the subscribing classes.


By:kamal
Date:2012-10-10 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Links

C interview questions and answers for experienced and fresher