Is there a common way to pass a single item of type T
to a method which expects an IEnumerable<T>
parameter? Language is C#, framework version 2.0.
Currently I am using a helper method (it's .Net 2.0, so I have a whole bunch of casting/projecting helper methods similar to LINQ), but this just seems silly:
public static class IEnumerableExt
{
// usage: IEnumerableExt.FromSingleItem(someObject);
public static IEnumerable<T> FromSingleItem<T>(T item)
{
yield return item;
}
}
Other way would of course be to create and populate a List<T>
or an Array
and pass it instead of IEnumerable<T>
.
[Edit] As an extension method it would be (with a more appropriate name, as Dan said):
public static class IEnumerableExt
{
// usage: someObject.AsEnumerable();
public static IEnumerable<T> AsEnumerable<T>(this T item)
{
yield return item;
}
}
Am I missing something here?