What will be the result of the following code execution?
 
class Program 
{ 
    static IEnumerable<char> GetLetters() 
    { 
         yield return 'A'; 
         yield break; 
         yield return 'B'; 
         yield return 'C'; 
    } 


   static void Main(string[] args) 
   { 
       foreach(char ch in GetLetters()) 
       { 
            Console.Write(ch); 
       } 
       Console.ReadLine(); 
    } 
}
 
Explanation
Yield break operator ends the iteration. Operators yield return 'B' and yield return 'C' will never be executed. Read more there: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/iterators

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C# quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes