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

  • SEARCH
  • Home
  • 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 8488619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:35:28+00:00 2026-06-10T21:35:28+00:00

Subtitle: Can EventHandlerList key’s Type be something else than object? I wanted to use

  • 0

Subtitle: Can EventHandlerList key’s Type be something else than object?

I wanted to use an enum store the keys I would like to have in an EventHandler.

public enum EventKey
{
    OnBark, OnCry
}

public EventHandlerList EventList = new EventHandlerList();

public event ComplaintEventHandler OnBark
{
    add
    {
        EventList.AddHandler(EventKey.OnBark, value);
    }
    remove
    {
        EventList.RemoveHandler(EventKey.OnBark, value);
    }
}

var handler = EventList[eventKey] as ComplaintEventHandler;

>

handler = null

As it turns out it does not work. But it works if I use keys declared like (as shown on):

static object EventKeyOnTap = new object();

After reading some mscorlib’s code I see that the problem comes from next.key == key in

private EventHandlerList.ListEntry Find(object key)
{
    EventHandlerList.ListEntry next = this.head;

    while (next != null && next.key != key)
    {
        next = next.next;
    }
    return next;
}

Both compared keys are from my Enum, but they are not equal!
I guess it comes from some implicit casts to object happening (the key stored in the list is of type object) but am not fluent enough with such low-level concepts.

Is my guess right?

What is the best way to use an Enum as a key in an EventHandlerList?

For now I will create my own EventHandlerList with Enum as key Type.

For now I created my own EventHandlerList with a constructor taking a Func<object, object, bool> which I then use in place of the equality comparaison aforementioned.

  • 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-10T21:35:30+00:00Added an answer on June 10, 2026 at 9:35 pm

    Try this code. Can you explain output?

    var bark1 = (object)EventKey.OnBark;
    var bark2 = (object)EventKey.OnBark;
    
    Console.WriteLine(bark1 != bark2);
    Console.WriteLine(bark1.Equals(bark2));
    

    If yes, I don’t know why you asked this question. If no, you definitely should get aware of value types, reference types and boxing.

    In short, AddHandler method accepts object parameter, hence your key (which is value type) is boxed when you call:

    EventList.AddHandler(EventKey.OnBark, value);
    

    If you call this method twice with the same enum key, key will be boxed twice, and two different objects in heap will be actually created.

    That’s why this check next.key != key inside Find method fails (it compares addresses of two separate objects in heap).

    EventHandlerList is sealed class, so you cannot affect its guts, but in your own code you could handle this situation with better check:

    next.key.Equals(key)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a custom ArrayAdapter so I can use the Title/Subtitle view that
I'm trying to make a function that can take an enum type, display all
I am using a predicate to find an object in core data. I can
I am trying to figure out how i can add an object to a
Can we create some custom button with title and subtitle text in it. Title
Can someone tell me what's the font family names for above texts (title, subtitle,
I can define a style as follows in Silverlight 4: <Style x:Name=Subtitle TargetType=TextBlock> <Setter
I'm wanting to do something like SELECT t.subtitle FROM temp t LEFT JOIN ep
How can I tidy multiple if condition for readability? For instance, if( $key !=
I have a chart in Excel that I'd like to use a subtitle on.

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.