> For the complete documentation index, see [llms.txt](https://andreyakinshin.gitbook.io/problembookdotnet/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://andreyakinshin.gitbook.io/problembookdotnet/ru/linq/enumerabletoarray-s.md).

# «EnumerableToArray» (Решение)

## Ответ

```
GetString: Foo
EnumerableToArray: Foo
GetString: Bar
EnumerableToArray: Bar
GetString: Foo
GetString: Bar
```

## Объяснение

LINQ-запросы являются ленивыми, т.е. реализуют отложенное исполнение. Это означает, что если сфомировать запрос и не вызвать для него явно метод вроде [ToArray()](https://github.com/andreyakinshin/problembookdotnet/tree/58c938c3d3cd146f5380f906225b46237a28506e/ru/Linq/msdn.microsoft.com/library/vstudio/bb298736.aspx) или [ToList()](http://msdn.microsoft.com/library/vstudio/bb342261.aspx), то выполнение запроса будет отложено до того момента, пока мы явно не затребуем результатов. Таким образом, строка

```csharp
var strings = GetStringEnumerable();
```

не выведет на консоль ничего. Далее, в цикле

```csharp
foreach (var s in strings)
  Console.WriteLine("EnumerableToArray: " + s);
```

произойдёт выполнение запроса. Причём, сначала выполнится первый `yield` (вывод строки `GetString: Foo`), а после него выполнится тело цикла для первого значения перечисления (вывод строки `EnumerableToArray: Foo`). Далее, цикл `foreach` запросит второй элемент перечисления, будет выполнен второй `yield` (вывод строки `GetString: Bar`) и второй раз будет выполнено тело цикла для полученного элемента (вывод строки `EnumerableToArray: Bar`).

Далее следует строка

```csharp
return strings.ToArray();
```

Тут можно наблюдать повторное исполнение LINQ-запроса, а значит мы вновь произойдёт вывод строк `GetString: Foo` и `GetString: Bar`.

[Задача](/problembookdotnet/ru/linq/enumerabletoarray-p.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://andreyakinshin.gitbook.io/problembookdotnet/ru/linq/enumerabletoarray-s.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
