The example you posted is basically an implementation of an enumerator, so yes it would work.
string[] _array = {"foo", "bar", "something", "else", "here"};
IEnumerable<String> GetEnumarable()
{
foreach(string i in _array)
yield return i;
}
If you want to do this with a custom data structure or want to put more logic when moving to the next element (i.e. lazy loading, streaming data) you can implement the IEnumerator
interface yourself.
Example
public class EnumeratorExample : IEnumerator
{
string[] _array;
// enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public EnumeratorExample(string[] array)
{
_array = list;
}
public bool MoveNext()
{
++position;
return (position < _array.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get { return Current; }
}
public string Current
{
get
{
try
{
return _array[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException("Enumerator index was out of range. Position: " + position + " is greater than " + _array.Length);
}
}
}
}
References
- IEnumerable Interface