Question

Finding myself in the situation where I have a method with this signature

void DoSomething(IEnumerable<T> before, IEnumerable<T> after)

I find myself often having to call it when I've just got one element and not IEnumerable.

I thought of adding the three overloads, but that doesn't help when one of the arguments is null.

So I thought I could just 'wrap up' my single element in an IEnumerable and send it on, but I can't find anything on Enumerable which looks like what I want (maybe I'm not looking hard enough).

So I'm looking at something like this

    public static IEnumerable<T> EnumerableUnit<T>(T item)
    {
        if(item != null) yield return item;
    }

Is this a good idea? Or is there something better I should be doing?

Was it helpful?

Solution

This might help:

new[]{obj}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top