ProblemBook.NET
  • ProblemBook.NET
  • LANGS
  • Задачник.NET
    • Math
      • «AugmentedAssignment» (Задача)
      • «Rounding1» (Задача)
      • «Overflow» (Решение)
      • «Overflow» (Задача)
      • «DivideByZero» (Решение)
      • «DivideByZero» (Задача)
      • «Eps» (Решение)
      • «DynamicIncrement» (Решение)
      • «Eps» (Задача)
      • «DynamicIncrement» (Задача)
      • «Rounding2» (Решение)
      • «AugmentedAssignment» (Решение)
      • «Rounding1» (Решение)
      • «Rounding2» (Задача)
    • Oop
      • «InheritanceNestedClass» (Задача)
      • «OverloadResolutionBasic» (Решение)
      • «OverloadResolutionOverride» (Решение)
      • «OverloadResolutionBasic» (Задача)
      • «OverloadResolutionOverride» (Задача)
      • «OverloadResolutionInheritance» (Решение)
      • «OverloadResolutionInheritance» (Задача)
      • «StaticFieldInGenericType» (Решение)
      • «StaticFieldInGenericType» (Задача)
      • «InheritanceNestedClass» (Решение)
    • ValueTypes
      • «Enumerator» (Решение)
      • «MutableProperty» (Задача)
      • «Enumerator» (Задача)
      • «StructLayout» (Решение)
      • «StructLayout» (Задача)
      • «Boxing» (Решение)
      • «Boxing» (Задача)
      • «MutableProperty» (Решение)
    • Strings
      • «InternmentAndMetadata» (Решение)
      • «CorruptedNumber» (Решение)
      • «InternmentAndMetadata» (Задача)
      • «TextElementEnumerator» (Решение)
      • «CorruptedNumber» (Задача)
      • «CaseInComparison» (Решение)
      • «TextElementEnumerator» (Задача)
      • «CaseInComparison» (Задача)
      • «StableSorting» (Решение)
      • «Secure» (Решение)
      • «CurrentCulture» (Решение)
      • «StableSorting» (Задача)
      • «Secure» (Задача)
      • «StringPlusNull» (Решение)
      • «CurrentCulture» (Задача)
      • «ExplicitlyInternment» (Решение)
      • «PlusChar» (Решение)
      • «StringPlusNull» (Задача)
      • «ExplicitlyInternment» (Задача)
      • «PlusChar» (Задача)
      • «NoStringInterning» (Решение)
      • «CorruptedString» (Решение)
      • «PlusString» (Решение)
      • «CompareToVsEquals» (Решение)
      • «NoStringInterning» (Задача)
      • «PlusString» (Задача)
      • «CorruptedString» (Задача)
      • «CompareToVsEquals» (Задача)
    • Linq
      • «QueryAfterRemove» (Решение)
      • «ClosureAndVariable» (Решение)
      • «QueryAfterRemove» (Задача)
      • «ClosureAndVariable» (Задача)
      • «EnumerableToArray» (Решение)
      • «QueryWithInc» (Решение)
      • «EnumerableToArray» (Задача)
      • «QueryWithInc» (Задача)
      • «LifeAfterYield» (Решение)
      • “YieldExceptionYield” (Решение)
      • «LifeAfterYield» (Задача)
      • «ExceptionYieldYield» (Решение)
      • «ClosureAndForeach» (Решение)
      • «TryYieldFinally» (Задача)
      • «ClosureAndFor» (Решение)
      • «ExceptionYieldYield» (Задача)
      • «YieldExceptionYield» (Решение)
      • «ClosureAndForeach» (Задача)
      • «ClosureAndFor» (Задача)
      • «YieldExceptionYield» (Задача)
    • Multithreading
      • «ThreadStaticVariable» (Решение)
      • «ThreadStaticVariable» (Задача)
      • «LockSlimInFinalizer» (Решение)
      • «LockSlimInFinalizer» (Задача)
    • Summary
    • Введение
  • ProblemBook.NET
    • Oop
      • “OverloadResolutionInheritance” (Solution)
      • “InheritanceNestedClass” (Solution)
      • “OverloadResolutionInheritance” (Problem)
      • “InheritanceNestedClass” (Problem)
      • “OverloadResolutionOverride” (Solution)
      • “OverloadResolutionOverride” (Problem)
      • “StaticFieldInGenericType” (Solution)
      • “OverloadResolutionBasic” (Solution)
      • “StaticFieldInGenericType” (Problem)
      • “OverloadResolutionBasic” (Problem)
    • Strings
      • “CorruptedNumber” (Problem)
      • “PlusString” (Solution)
      • “TextElementEnumerator” (Problem)
      • “PlusString” (Problem)
      • “StableSorting” (Solution)
      • “StableSorting” (Problem)
      • “CaseInComparison” (Solution)
      • “CaseInComparison” (Problem)
      • “NoStringInterning” (Solution)
      • “Secure” (Solution)
      • “CorruptedString” (Solution)
      • “CompareToVsEquals” (Solution)
      • “CurrentCulture” (Solution)
      • “NoStringInterning” (Problem)
      • “Secure” (Problem)
      • “CorruptedString” (Problem)
      • “CompareToVsEquals” (Problem)
      • “StringPlusNull” (Solution)
      • “CurrentCulture” (Problem)
      • “ExplicitlyInternment” (Solution)
      • “PlusChar” (Solution)
      • “InternmentAndMetadata” (Solution)
      • “CorruptedNumber” (Solution)
      • “StringPlusNull” (Problem)
      • “ExplicitlyInternment” (Problem)
      • “PlusChar” (Problem)
      • “InternmentAndMetadata” (Problem)
      • “TextElementEnumerator” (Solution)
    • Linq
      • “YieldExceptionYield” (Problem)
      • “QueryAfterRemove” (Problem)
      • “ClosureAndVariable” (Problem)
      • “QueryWithInc” (Solution)
      • “QueryWithInc” (Problem)
      • “LifeAfterYield” (Solution)
      • “LifeAfterYield” (Problem)
      • “EnumerableToArray” (Solution)
      • “EnumerableToArray” (Problem)
      • «YieldExceptionYield» (Solution)
      • “ExceptionYieldYield” (Solution)
      • “ClosureAndForeach” (Solution)
      • “TryYieldFinally” (Problem)
      • “ClosureAndFor” (Solution)
      • “ExceptionYieldYield” (Problem)
      • “YieldExceptionYield” (Solution)
      • “ClosureAndForeach” (Problem)
      • “ClosureAndVariable” (Solution)
      • “QueryAfterRemove” (Solution)
      • “ClosureAndFor” (Problem)
    • Multithreading
      • “ThreadStaticVariable” (Solution)
      • “LockSlimInFinalizer” (Solution)
      • “ThreadStaticVariable” (Problem)
      • “LockSlimInFinalizer” (Problem)
    • ValueTypes
      • “StructLayout” (Problem)
      • “MutableProperty” (Solution)
      • “Boxing” (Solution)
      • “MutableProperty” (Problem)
      • “Boxing” (Problem)
      • “Enumerator” (Solution)
      • “Enumerator” (Problem)
      • “StructLayout” (Solution)
    • Math
      • “Rounding2” (Solution)
      • “AugmentedAssignment” (Solution)
      • “Rounding1” (Solution)
      • “Rounding2” (Problem)
      • “Rounding1” (Problem)
      • “AugmentedAssignment” (Problem)
      • “DivideByZero” (Solution)
      • “DivideByZero” (Problem)
      • “DynamicIncrement” (Solution)
      • “DynamicIncrement” (Problem)
      • “Eps” (Solution)
      • “Eps” (Problem)
      • “Overflow” (Solution)
      • “Overflow” (Problem)
    • Summary
    • Introduction
  • Attribution-NonCommercial-NoDerivatives 4.0 International
Powered by GitBook
On this page
  • Answer
  • Explanation

Was this helpful?

  1. ProblemBook.NET
  2. Linq

“LifeAfterYield” (Solution)

Answer

BarBaz

Explanation

The code from the problem will be transformed in the following code (some part of the code specifically removed for better understanding):

private sealed class FooEnumerable : 
    IEnumerable<string>, IEnumerator<string>
{
  private int state;
  public string Current { get; private set; }

  object IEnumerator.Current
  {
    get { return Current; }
  }

  public FooEnumerable(int state)
  {
    this.state = state;
  }

  public IEnumerator<string> GetEnumerator()
  {
    FooEnumerable fooEnumerable;
    if (state == -2)
    {
      state = 0;
      fooEnumerable = this;
    }
    else
      fooEnumerable = new FooEnumerable(0);
    return fooEnumerable;
  }

  IEnumerator IEnumerable.GetEnumerator()
  {
    return GetEnumerator();
  }

  bool IEnumerator.MoveNext()
  {
    switch (state)
    {
      case 0:
        Current = "Bar";
        state = 1;
        return true;
      case 1:
        state = -1;
        Console.WriteLine("Baz");
        break;
    }
    return false;
  }

  void IEnumerator.Reset()
  {
    throw new NotSupportedException();
  }

  void IDisposable.Dispose()
  {
  }
}

IEnumerable<string> Foo()
{
  return new FooEnumerable(-2);
}

void Main()
{
  var enumerator = Foo().GetEnumerator();
  while (enumerator.MoveNext())
    Console.Write(enumerator.Current);
}
Previous“QueryWithInc” (Problem)Next“LifeAfterYield” (Problem)

Last updated 6 years ago

Was this helpful?

Problem