By 22/07/2015 7 Comments

C# Events

  • C# events areĀ automatic notifications/actions which areĀ fired when a user interacts with an application.
  • An event is works like this: Any object which fires an event, it first registers an event handler for that event. When an event is occurred all that registered handlers are called.
  • Events are the members of a class and can have access modifiers.
  • Event are declared using a keyword event.
  • Delegates are the basic fundamental of the events. Delegates support the events and activate them.
  • Delegates represent the event handlers.
  • Event handlers are methods which can be static or instance/non-static methods.
  • An eventhandler is must be added/registered to an event before call it. If an event is called before an event-handlers registration a run-time null reference exception will be generated.
  • A Handler is registered/added to an event using the += operator because events support += and -= operators to registered or unregistered handlers to the events.
  • An event has a delegate as its return type. A delegate also defines the signature and parameters for an event.

General Syntax:

event delegateName EventName;

STATEMENT DESCRIPTION
event It is a keyword.
delegateName Name of a delegate.
EventName An event name, it should be any meaningful name.

Example:

using System;
namespace csharpAdvance
{
    // Void type delegate.
    delegate void ClickHandel();

    class Calculator
    {
        // Public event ClickAdd of delegate type is declared inside class.
        public event ClickHandel ClickAdd;

        // public void type method which calls an event.
        public void OnAddition()
        {
            if (ClickAdd != null)
                ClickAdd();
            else
                Console.WriteLine("Please register a handler to an event !");
        }
    }

    // A class declaration which has a static method as a handler for an event.
    class CalculatorHandlers
    {
        public static void AdditionHandler()
        {
            Console.WriteLine("Addition answer: {0}", (10 + 10));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Calculator calculator = new Calculator();

            // A handler is registered/added to an event by += operator.
            calculator.ClickAdd += CalculatorHandlers.AdditionHandler;

            // ClickAdd event is triggered by calling OnAddition() method.
            calculator.OnAddition();
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Addition answer: 20
     */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .