Let’s say I have a user control named A that does the following:
Page_Load:
List<object> myList = PrepareList();
ListPrepared(this, new AArgs(myList)); // Event that lets subscribers modify the list.
OperateOnAndDisplayList(myList);
An instance of A is held in a page that subscribes to the ListPrepared event and modifies the list that’s created. I want the page’s handler to finish before I operate on and display the list, obviously, since it’s modifying the list for completeness. How would I go about implementing this? (Or is this a horrible idea, since it exposes the list and thus requires outside elements to have knowledge of the list in order to modify it?)
Events are raised synchronously. This means that all subscribers to the event will execute their code before the
OperateOnAndDisplayListmethod is called. This also means that ifmyListis modified by any of the event subscribers, the modification will be visible to the event publisher as well. If this isn’t what you want, you should create a copy of the list before raising the event, and only send the copy with the event’sEventArgs.