Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:06:49+00:00 2026-06-09T15:06:49+00:00

In class’s derived from a Component I sometimes see events declared like: private static

  • 0

In class’s derived from a Component I sometimes see events declared like:

private static readonly object LoadEvent = new object();
public event EventHandler<MyEventArgs> Load
{
    add { Events.AddHandler(LoadEvent, value); }
    remove { Events.RemoveHandler(LoadEvent, value); }
}

protected virtual void OnLoad(MyEventArg e)
{
    var evnt = (EventHandler<MyEventArg>)Events[LoadEvent];
    if (evnt != null) 
        evnt(this, e);
}

Instead of just:

public event EventHandler<MyEventArgs> Load;

protected virtual void OnLoad(MyEvent e)
{
   if (Load != null)
       Load(this, e);
}

I’m tempted to to refactor to use the shorter method, but I am hesitant in case there are some advantages to using the Component EventHanderList that I am missing.

The only advantages I can currently think of are:

  • When the component is disposed, all items in the EventHandlerList are removed, effectively automatically unhooking event handlers.
  • Possibly less memory fragmentation because of all the attached delegates going into the single EventHandlerList.

Is there anything else?

(This is not a question about the general use of explicit add + removes on events.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:06:50+00:00Added an answer on June 9, 2026 at 3:06 pm

    This is good for sparse events. UI controls tend to have dozens (sometimes upwards of 100) events. If a field-like-event was used for each, then each event requires a reference backing-field. With 100 events, that is 400 bytes on x86 or 800 bytes on x64, even if not a single event is subscribed.

    As an example, a winforms System.Windows.Forms.Form has 91 events before you’ve added any. Every single Control instance has at least 69.

    So; a windows form with a few labels, input boxes and buttons could easily have an extra 2000 reference fields (16k on x86) most of which are doing nothing.

    The EventHandlerList is essentially a key/value lookup, which means that if only 3 events are subscribed (I’m thinking “Click” and maybe a few others) then only a nominal amount of memory is required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class BaseA{}; , class DerivedB{}; class DerivedC{}; DerivedB can be derived from BaseA :
class Object { }; Class Derived : public Object { }; boost::shared_ptr<Object> mObject(new Derived);
class User extends ActiveRecord\Model { pubic static $primary_key = 'userId'; private function isUserLoggedIn() {}
Class 1 private void checkDuplicateCustomer(BulkCustomerVO bulkCustomerVO) { PagedDuplicateCustomerVO duplicateCustomerVO = new PagedDuplicateCustomerVO(); duplicateCustomerVO.setCustomer(bulkCustomerVO.getCustomerVO()); duplicateCustomerVO
class Test { public static void main(String[] args) throws Exception { Test t =
class EverythingMustBeInAClass { private final int i = 42; private final int[] a =
{ class Program { static void Main(string[] args) { int id = 0, stock
class Program { static void Main(string[] args) { Console.WriteLine(Fib 1: ); Console.ReadLine(); } long
class Test extends JPanel implements MouseListener { private JButton b1, b2, b3, b4; public
class LinkedList{ private $first; public function Merge(LinkedList $ll){ //We can't access $ll->first for merging

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.