Here is a example of using yield:
public static IEnumerable<int> Unique(IEnumerable<int> nums){ Dictionary<int, int> uniqueVals = new Dictionary<int, int>(); foreach (int num in nums) { if (!uniqueVals.ContainsKey(num)) { uniqueVals.Add(num, num); yield return num; } }}Dompare the typical way and the "yield" to get IEnumerable
# IList<string> FindBobs(IEnumerable<string> names) # { # var bobs = new List<string>(); # # foreach(var currName in names) # { # if(currName == "Bob") # bobs.Add(currName); # } # # return bobs; # }# IEnumerable<string> FindBobs(IEnumerable<string> names) # { # foreach(var currName in names) # { # if(currName == "Bob") # yield return currName; # } # }
# IEnumerable<string> FindBobs(IEnumerable<string> names) # { # foreach(var currName in names) # { # if(currName == "Bob") # yield return currName; # } # }