1

Lets say i have a method:

 UpdateObject(IList<MyObject> objs)

and i want to use that method for sending just one object? (i prefer not using method overloading cause i dont want a lot of methods to handle in my interface)

is there a way i could use the same method without doing the following:

UpdateObject(new List<MyObject>(){singleObj})
Servy
  • 202,030
  • 26
  • 332
  • 449
Ori Price
  • 3,593
  • 2
  • 22
  • 37
  • Chances are you'll be calling this method on the interface more often than you'll be implementing it. The cost of adding an extra overload is probably cheaper than almost any change on the caller's side (when aggregated across all instances). – Servy Oct 04 '12 at 16:33

1 Answers1

3

Maybe you could pass an IEnumerable<MyObject> to that method instead.

Probably the easiest way to append a single element to an IEnumerable<T> is to use this AsIEnumerable extension (although Eric Lippert advises against creating extension for objects):

public static IEnumerable<T> AsIEnumerable<T>(this T obj)
{
    yield return obj;
} 

Now you can use everything as IEnumerable.

UpdateObject(singleObj.AsIEnumerable())
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939