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 8167603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:14:47+00:00 2026-06-06T20:14:47+00:00

I have such class (sorry about posible mistakes, i’m writing it right here.) Class

  • 0

I have such class (sorry about posible mistakes, i’m writing it right here.) Class is simplified for this example, it must be more complex of course.

class SP500Index {

    SP500Index(List<OrderBook> stocks) {
        foreach (var stock in stocks) {
            stock.StockUpdated += stockUpdated; // how to handle?
        }
    }

}

So I have a lot of sources and I need to handle StockUpdated event from them. In handler I need to know index of stock in stocks list which raised the event. How to do that?

upd for perfomance reasons I don’t want “sender look-up” instead I want index. Lookup is not trivial operation and likely involves Hashcode calculation Equals method call etc. Imagine how ofthen SP500 index changes…

  • 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-06T20:14:49+00:00Added an answer on June 6, 2026 at 8:14 pm

    If your benchmark shows that you need the index when the event occurs you can add the index as a property to OrderBook and when you add an element to the list, set this property. This value will be available to the event handler.

    This will work assuming that you keep the OrderBook objects in a single List and do not do any rearranging of this original list. If you have multiple lists with each object stored on only one of those lists, then you could add an Owner property that references the list that it is stored on.

    For example…

    When you build List<OrderBook>:

    ...
    OrderBook book = CreateOrderBook(...);
    list.Add(book);
    book.ListIndex = list.Count - 1;
    // Assign Owner here if that is needed.
    ...
    

    Or better, use a helper for managing the list that cares for the book keeping of the index update:

    public class OrderBookManager
    {
        private List<OrderBook> list = new List<OrderBook>();
    
        public void Add(OrderBook book)
        {
            list.Add(book);
            book.ListIndex = list.Count - 1;
            // Assign Owner here if that is needed.
        }
    
        // Make this read-only if you want to ensure the manager controls all updates to the list (better design) but use it this way for higher performance.
        public List<OrderBook> List { get { return list; } }
    }
    

    The updated OrderBook:

    public class OrderBook
    {
        ...
    
        public int ListIndex { get; set; }
    }
    

    And then an sample event handler using this index:

    public void StockUpdated(object sender, MyEventArgs eventArgs)
    {
        OrderBook book = (OrderBook) sender;
        //Here use book.ListIndex to access the original list element.
    }
    

    What is your reason for having that index? Everything you need should be in the object. If you use this to manipulate the original list (such as removing this item from the list) then you have the problem of recomputing the saved index of all the previously stored objects.

    If you are maintaining a parallel list with other objects, then you perhaps should consider a different design.

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

Sidebar

Related Questions

Sorry for asking what seems like such an obvious question. I have an adapter
My question (which will follow after this, sorry about the long intro, the question
I have case where the class hierarchy is something like this, +---------------+ | UIElement
I have a class Cars and class Models. Car has many properties such as
Sorry this is such a long question. Ive been doing lots of research lately
I'm a complete beginner with jQuery and I have this bit of script here
Hey, sorry to be such a nuisance, but I have another jQuery problem. Same
How can I set a global variable in this class? I have tried this:
For example, I have such a code: <?php $A = array( 'echoSmth' => function(){
Yeah, sorry about asking a stupid n00b question. So I have a C# program.

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.