Sail E0 Webinar
Question
What will the given code snippet specify?class MyClass{    char chrs = 'A' ;    public IEnumerator GetEnumerator()    {        for (int i = 20; i >=0; --i)        if (i == 10) yield break;        yield return (char)((chrs + i));    }}class Program{    static void Main(string[] args)    {        MyClass mc = new MyClass();        foreach (char ch in mc)        Console.Write(ch + " ");        Console.WriteLine();        Console.ReadLine();    }}
Options:
A .  Code run successfully prints nothing
B .  A B C D E F G H I J K L M N O P Q R S T U V
C .  U T S R Q P O N M L
D .  Compile time error
Answer: Option C


The code to specify stoppage of the iterator using 'yield break' statement When this statement executes, the iterator signals that the end of the collection has been reached, which effectively stops the iterator.
Output: U T S R Q P O N M L



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers