Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With this .NET event, is it OK to pass in this IList instance?

i've got the following code :-

while (....)
{
    var foo = DoTheFooShakeShakeShake(..);
    foos.Add(foo); // foos is an IList<Foo>, btw and is new'd, above.

    if (foos.Count % 100 == 0)
    {
        var e = new CustomFooEventArgs { UserId = whatever, Foos = foos };
        OnFooPewPew(this, e);
        foos.Clear();
    }
}

// We still might have some foo's left over.. so send em off also.
// Excuse the woeful var names, below.
var e2 = new CustomFooEventArgs { UserId = whatever, Foos = foos };
OnFooPewPew(this, e2);

So, i grab all the foo's for some while/loop condition. Every 100 foo's i then fire an event, which passes the list of foo's off to the subscriber. I then clear this list of foos. Once the loop is finished, i then fire off any remaining foo's to the subscriber.

So - if i fire off an event, which contains the list of foo's ... and then i CLEAR that list .. will that mean that the subscriber can possibly get that list, which is now empty? Should I be passing across a COPY of the list ... and then clearing the original list?

like image 585
Pure.Krome Avatar asked Dec 28 '22 07:12

Pure.Krome


2 Answers

Forget about clearing, the fact that you're mutating the list at all is going to wreak havoc for objects that subscribe to your event. If they persist the list that is returned, you're going to be changing the data anytime you add to the list or clear it.

Not only can you mess the subscriber up, they can mess with you, as they can mutate the list and affect your own process.

If that is not the desired behavior (and I can't think that it is), you're going to want to send not even a copy, but an IEnumerable<Foo> or ReadOnlyCollection<Foo>. Because even if you send a copy, if you have multiple subscribers, they'll all receive the same copy, so their mutations will still wreak havoc for one another.

like image 137
Anthony Pegram Avatar answered Jan 15 '23 17:01

Anthony Pegram


By the time you clear the list, the event handlers will already have been executed (they're called synchronously), so it's not an issue.

However, you should avoid passing the list itself, you should pass a copy. Otherwise the event subscribers can keep a reference to the list and mess with it in unpredictable ways...

like image 43
Thomas Levesque Avatar answered Jan 15 '23 18:01

Thomas Levesque