Showing posts with label events. Show all posts
Showing posts with label events. Show all posts

Monday, March 3, 2014

C# - Declaring Events

Events are closely related to delegates, but they are not the same thing. An event allows code to subscribe and unsubscribe using delegate instances as event handlers. The idea is that when an event is raised all the event handlers which have subscribed to the event are called. Just as a property is logically just two operations get and set, an event is also logically just two operations: subscribe and unsubscribe.


To declare an event and explicitly write these operations, you use syntax which looks like a property declaration but with add and remove instead of get and set:

Public event EventHandler CustomEvent
{
          Add
          {
                   //Implementation goes here: “Value” variable is the handler being
//subscribed to the event
          }
          Remove
          {
                   //Implementation goes here: “Value” variable is the handler being
//unsubscribed to the event
          }
}

Many events are implemented using a simple variable to store the subscribed handlers.

C# allows these events to be created simply, as field-like events:
          Public event EventHandler SimpleEvent;
This declares both the event and a variable at the same time.
It’s roughly equivalent to this:

            private EventHandler_hiddenField;
public event EventHandler SimpleEvent
{
          add
          {
                   lock(this)
                   {
                             _hiddenField += value;
                             }
                   }
                   remove
                   {
                   lock(this)
                   {
                             _hiddenField -= value;
                             }
                   }
          }

Everywhere you refer to SimpleEvent within the declaring type, the compiler actually reference _hiddenField, which is why you’re able to raise the event by calling SimpleEvent(). Outside the type declaration, however, SimpleEvent only refers to the event. This duality has caused confusion for many developers – you just need to remember that fields and events really are very different things, and field, like events are just the compiler doing some work for you.

Sunday, February 16, 2014

C# - Delegates

What is Delegates?
In a simple language, as we use in C and C++, a pointer which points to the functions. In C# there is no pointer, but delegates are working as a pointer to function.
A delegate is a reference type variable that holds the reference to a method. The reference can be changed at run-time.

Use of Delegates
When we want to use functionality of Events and Call-backs at that time delegates comes to the picture.
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs.

Declaration of Delegates
Declaration of delegates determines the methods that can be referenced by the delegate.
All methods which are refers to delegate must have same signature as delegate.


          delegate   <return type>   <delegate-name>    <parameter list>
public    delegate       int            MyDelegate         (string s);

Example
 

using System;

delegate int Calc(int n);
namespace DelegateAppl
{
   class TestDelegate
   {
      static int num = 0;
      public static int AddNum(int p, int q)
      {
         num = p + q;
         return num;
      }

      public static int MultNum(int p, int q)
      {
         num = p * q;
         return num;
      }
      public static int getNum()
      {
         return num;
      }

      static void Main(string[] args)
      {
         //create delegate instances
         Calc c1 = new Calc(AddNum);
         Calc c2 = new Calc(MultNum);
         //calling the methods using the delegate objects
         c1(25,30);
         Console.WriteLine("Value of Num: {0}", getNum());
         c2(5,6);
         Console.WriteLine("Value of Num: {0}", getNum());
         Console.ReadKey();
      }
   }
}


Output:
Value of Num: 55
Value of Num: 30
  



Freemarket.com Marketplace