Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. New Snippets Site

New Snippets Site

Scheduled Pinned Locked Moved The Lounge
visual-studiocom
22 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Marc Clifton

    Leslie Sanford wrote:

    Here's a webpage[^] that goes into a little detail.

    That's quite interesting! Thanks for the link. But what I don't understand is, why isn't:

    EventHandler handler = MyEvent;

    just a reference copy of the invocation list? Marc VS2005 Tips & Tricks -- contributions welcome!

    L Offline
    L Offline
    Leslie Sanford
    wrote on last edited by
    #21

    Delegates/events are immutable, so they have the same semantics as using the string class, for example. Consider:

    using System;
    using System.Diagnostics;

    namespace Tester
    {
    class Class1
    {
    public delegate void MyEventHandler(object sender, EventArgs e);

        public static event MyEventHandler MyEvent;
    
        private static int invocationCount = 0;
    
        private static void HandleMyEvent(object sender, EventArgs e)
        {
            Console.WriteLine("Handling event...");
            invocationCount++;
        }
    	
        \[STAThread\]
        static void Main(string\[\] args)
        {
            MyEventHandler handler0 = MyEvent;
    
            MyEvent += new MyEventHandler(HandleMyEvent);
    
            MyEventHandler handler1 = MyEvent;
    
            MyEvent += new MyEventHandler(HandleMyEvent);
    
            MyEventHandler handler2 = MyEvent;
            Debug.Assert(handler0 == null);
            Debug.Assert(handler1 != null);
            Debug.Assert(handler1.GetInvocationList().Length == 1);
            Debug.Assert(handler2 != null);
            Debug.Assert(handler2.GetInvocationList().Length == 2);
    
            handler1(null, EventArgs.Empty);
            handler2(null, EventArgs.Empty);
    
            Debug.Assert(invocationCount == 3);            
        }
    }
    

    }

    With the output:

    Handling event...
    Handling event...
    Handling event...

    M 1 Reply Last reply
    0
    • L Leslie Sanford

      Delegates/events are immutable, so they have the same semantics as using the string class, for example. Consider:

      using System;
      using System.Diagnostics;

      namespace Tester
      {
      class Class1
      {
      public delegate void MyEventHandler(object sender, EventArgs e);

          public static event MyEventHandler MyEvent;
      
          private static int invocationCount = 0;
      
          private static void HandleMyEvent(object sender, EventArgs e)
          {
              Console.WriteLine("Handling event...");
              invocationCount++;
          }
      	
          \[STAThread\]
          static void Main(string\[\] args)
          {
              MyEventHandler handler0 = MyEvent;
      
              MyEvent += new MyEventHandler(HandleMyEvent);
      
              MyEventHandler handler1 = MyEvent;
      
              MyEvent += new MyEventHandler(HandleMyEvent);
      
              MyEventHandler handler2 = MyEvent;
              Debug.Assert(handler0 == null);
              Debug.Assert(handler1 != null);
              Debug.Assert(handler1.GetInvocationList().Length == 1);
              Debug.Assert(handler2 != null);
              Debug.Assert(handler2.GetInvocationList().Length == 2);
      
              handler1(null, EventArgs.Empty);
              handler2(null, EventArgs.Empty);
      
              Debug.Assert(invocationCount == 3);            
          }
      }
      

      }

      With the output:

      Handling event...
      Handling event...
      Handling event...

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #22

      Leslie Sanford wrote:

      Delegates/events are immutable, so they have the same semantics as using the string class, for example.

      Ah! Thanks for the example. It's definitely not obvious from the symantics of the language. Marc VS2005 Tips & Tricks -- contributions welcome!

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups