Q:

Cheapest way to make a single object Enumerable?

I have an interface function that returns an IEnumerable<MyObject> collection.
Some of my classes only use one hardcoded instance of MyObject, but still need to implement the interface.
Currently I am just creating a System.Array like so:
public class SomeClass : IMyInterface
{
    private MyObject _myObject;

    public IEnumerable<MyObject> IMyInterface.GetMyObjects()
    {
        return new MyObject[] { _myObject };
    }
}

I just wondered if there was a more efficient way, or any magic to cast the object itself?

A:

You may just...
public IEnumerable<MyObject> IMyInterface.GetMyObjects()
{
    yield return _myObject;
}

...but consider that this will cause the compiler to generate a whole state machine class for it to work. If that sounds cheap for you, OK. Otherwise, the array solution looks fine. You may want to pre-create the array if there's no problem in reusing its instance.

