Cost of passing an IEnumerable with only one element vs passing the element itself

I am developing a data collection system that has some event based data streams.

Typically, there is a class NewDataAvailableArgs

that returns one or more new values.

I doubt if I should have two different arg classes like below:

class NewManyDataAvailableArgs
{
    public IEnumerable<int> Values { get; private set; }

    public NewDataAvailableClass(IEnumerable<int> values)
    {
        values = Values;
    }
}

class NewSingleDataAvailableArgs
{
    public int Value { get; private set; }

    public NewDataAvailableClass(int value)
    {
        value = Value;
    }
}

      

Or should I stop worrying and just use a collection with one item if I only have one value to pass? I'm afraid, for example, that I end up with too many events, such as the (possibly unnecessary) creation of a new array just because the constructor signature says so:

Something.NewDataAvailable(this, new DataAvailable(new int[] { single_value });

      

Should I prefer one signature or this kind of "overload" of events? Will it be a matter of performance, style, or just a matter of preference?

+3


source to share


2 answers


Are clients specifically satisfied with the fact that there will be one item, or are they just processing the collection, no matter how many items there are? A specialized event is only a good idea if there is a consumer using it. If all the consumer does is foreach over the items and print them, there is no need for a custom event.



In terms of performance, I can imagine a single price version can keep the selection, but the architectural cost seems high.

+6


source


I think the best way to go here is to use one case of IEnumerable, since the memory overhead is not that big, there will be just a few extra bytes for a pointer with respect to one value you have stored. This is a trade-off that you must make if you want to put all the unique streaming collections of a collection on one line. Long story short, yes, you lose some probably unnoticeable performance, but you gain code readability.



+2


source







All Articles