c# - Does a code that combines single() with yield make any sense? -


i came across code should return single object expected in list, code has iterator block yields found items, have simplified case in following example:

private static void methode_a() {     console.write("{0} ", numberlist(2, 8).single());      }  private static ienumerable<int> numberlist(int min, int max) {     while (min < max)         yield return min++;         } 

does make sense or there known benefits of combining single() yield on building list , asserting contains 1 single element?

the way works following:

  1. you call numberlist(2, 8).single()
  2. the execution flow enters while loop first time , returns first result (2).
  3. the execution flow continues on second iteration of loop , returns second element (3)
  4. the .single method throws exception , stops execution because expected there 1 element contained in enumerator

on other hand if call numberlist(2, 8).first() here's happen:

  1. the execution flow enters while loop first time , returns first result (2).
  2. the execution flow no longer continues in while loop because 1 element returned , element result of call

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -