public static {ReturnType} GetNums()
{
var number = 0;
while (true)
{
if (number > 5)
yield break;
yield return number++;
}
}
a) void
b) IEnumerable<int>
c) List<int>
d) int
e) object
Expand the correct answer with an explanation
This method uses two special keywords yield break
and yield return
. The method in which such keywords are used is called an iterator.
The iterator can be named (as in our case) or unnamed. An unnamed iterator is declared for the class by defining the GetEnumerator()
method. In this case, instances of this class can be used directly in foreach or linq operations.
The difference between an iterator and a regular method is that the method returns a single value with return
, and the iterator returns several (or none at all). An iterator will return as many values as the yield return
is called. The yield return
call can be written several times in a row or, as in the task, use a loop. The yield break
keyword means that no more elements do need to be returned, even if the more yield return
follow. Stopping an iterator using a yield break
is optional. For example, we can rewrite the code from the task like this:
while (number < 5)
{
yield return number++;
}
Also interesting is the interception of errors in the iterator. The yield
keyword cannot be used inside a try-catch block. Instead, you must first get the value to return, and only then call the yield
, for example:
foreach (var item in data)
{
try
{
result = PrepareData(item);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
continue;
}
yield return result;
}
The previous paragraph already mentioned where you can use the result that the iterator returns – in the foreach construction and for linq operations. Therefore, the value returned by the iterator must be an IEnumerable interface. Typification for IEnumerable must correspond to the type that returns by yield return
. УIn our case it is IEnumerable<int>.
Correct answer: b) IEnumerable<int>.