Yield returns each element one at a time.
When to use:
- when calculate the next item in the list
- for infinite sets
Example
IEnumerable<KeyValuePair<string, string>> dataList = GetData();
dataList.ToList().ForEach(s => MessageBox.Show(s.Key + s.Value));
private IEnumerable<KeyValuePair<string, string>> GetData()
{
yield return new KeyValuePair<string, string>("1", "A");
yield return new KeyValuePair<string, string>("2", "B");
yield return new KeyValuePair<string, string>("3", "C");
}