Hi :)
i am having a hard time understanding the concept of event & delegate.

i think i understand that one object start an event and some other objects can react while the event start.

but why is that different from the observer design pattern?
or how is it different from get/set properties settings? (like set that when the X property of an object set something happened in other objects)

can someone answer those questions?
and if u have a simple example of implementation of that issue ill be glad to read it

thanks!!! :)

Recommended Answers

All 2 Replies

A delegate is the .Net equivalent of a function pointer. Basically it describes a function/method to be called (return type, parameters count, and parameter types) without actually being a function - sort of a prototype of a function/method. These allow your code to be able to describe a function without actually knowing what the function's name is or what it does. All that it needs is the same return type, parameter count, and parameter types. This is similar to what an interface does for classes, only it's an 'interface' for methods/functions.

An Event is a special type of delegate that allows object(s) to subscribe to it. When a condition is met, the event is called and the method executed. EventHandlers are used to subscribe to events - an event can have unlimited subscribers, and an EventHandler can subscribe to unlimited events. I can't really explain this much better than any event-driven programming .Net tutorial off google could do, unfortunately.

Here is an example of calling a delegate (from a console app) without invoking an event handler.
This example shows a call to the method ProcessSomething.
There are two instances where delegates are in play
1) Where todaysMethod is set
2) Where ProcessSomething() calls the display method

using System;
using System.Collections.Generic;
using System.IO;

namespace DW_404922_CS_CON
{
   class Program
   {
      private static void DoOutputToTempFile(string strData)
      {
         string strTempFileName = Path.GetTempFileName();
         StreamWriter fileOut = new StreamWriter(strTempFileName);
         fileOut.WriteLine(strData);
         fileOut.Close();
         File.Delete(strTempFileName);
      }

      private static void DoOutputToScreen(string strData)
      {
         Console.WriteLine(strData);
      }

      private static void ProcessSomething(Action<string> doSomething, string strData)
      {
         doSomething(strData);
      }

      static void Main(string[] args)
      {
         List<string> lst_strData = new List<string>()
         {
            "once upon a time",
            "four-score and seven years ago",
            "there once was a man from Peru",
            "the only thing we have to fear is fear itself",
            "give me liberty or give me death",
         };

         Action<string> todaysMethod = DoOutputToScreen;

         lst_strData.ForEach(s => ProcessSomething(todaysMethod, s));
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.