Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Container container = new Container();
Enumerator enumerator = container.GetEnumerator();
try
{
object element;
while (enumerator.MoveNext())
{
element = enumerator.Current;
// содержимое foreach
}
}
finally
{
IDisposable disposable = enumerator as IDisposable;
if (disposable != null)
disposable.Dispose();
}
public IEnumerable<string> Lines(string path) {
string line;
using (var file = File.OpenText(Path.Combine(basepath, path)))
while ((line = file.ReadLine()) != null)
yield return line;
}
А потом я задумался, в какой же момент этот файл будет закрыт… using (var disposable = new SomeDisposable())
{
yield return 1;
yield return 2;
// some code
}
}
Что за тэги «факты, особенности, тонкости»?
Интересные моменты в C# (foreach)