I came across these two code snippets while reading about IEnumerable interfaces. I would like to understand the exact difference between them in simple terms.
Snippet 1 : without yield,
public IEnumerator GetEnumerator()
{
// Return the array object's IEnumerator.
return carArray.GetEnumerator();
}
Snippet 2:with yield,
public IEnumerator GetEnumerator()
{
foreach (Car c in carArray)
{
yield return c;
}
}
Both the snippets do the same work, So whats so specific in using YIELD over here?
Thanks in advance :)