Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6132711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:08:10+00:00 2026-05-23T17:08:10+00:00

Since C# 2.0 we’ve been able to declare iterator blocks inside classes and return

  • 0

Since C# 2.0 we’ve been able to declare iterator blocks inside classes and return values from them using the yield return keywords. Behind the scenes the compiler has generously converted our rather simple iterator block into its own class, a state machine which fully implements either the IEnumerable or IEnumerator interface. Using ILSpy or Reflector you can get a glimps of what these classes look like.

In C# 3.5 Extension Methods were added to the language providing a simple means of writing static methods that will appear in existing classes or interfaces’ intellisense popup.

What I am interested in doing is combining these two concepts and in doing so I’m having a difficult time. My frustration centers around the fact that in an iterator block, the this keyword refers to the class that the iterator block is in instead of the compiler generated class which holds the implementation of the iterator block.

What I’d like to do is something like this:

public static class IteratorExtensions
{
    public static void DoSomething<T>(this IEnumerable<T> iterator)
    {
        Console.WriteLine("Running iterator block of type :" + typeof(T).Name);
    }
}

public class IteratorExample
{
    public IEnumerable<string> MyIterator()
    {
        yield return "Hello";
        this.DoSomething();
        yield return "World";
    }
}

Unfortunately in this context this refers to an instance of IteratorExample and not to an instance of the generated MyIterator class. Is there any way to get a reference in the iterator or will I have to rethink my plans?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:08:11+00:00Added an answer on May 23, 2026 at 5:08 pm

    No clean way, no. All this usage is intercepted and refers to the declaring instance.

    You could possibly do something horrible involving abusing the stack or similar, but nothing tidy is obvious. You could maybe chain the iterators LINQ-style instead? i.e.

    public static IEnumerable<T> WithLogging<T>(this IEnumerable<T> source)
    {
        source.DoSomething();
        try {
            foreach(var item in source) yield return item;
        } finally {
            source.DoSomethingElse();
        }
    }
    

    and use originalIterator.WithLogging()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since it's impossible to capture text from a java canvas by using standard Windows
Since Graduating from a very small school in 2006 with a badly shaped &
Since upgrading a website from ASP.NET 3.5 to ASP.NET 4.0 my dates come out
Since I started using Fireworks CS5.1, and it doesn't matter how I write the
Since I'm using Java 1.4.2, this means that I cannot use Java's implementation of
Since I started learning Objective-C and Cocoa, I've been wondering why they have chosen
Since the C# using statement is just a syntactic sugar for try/finally{dispose}, why does
Since I am new to using Unity 2.0, I need some pointer or suggestion
since last week I've been busy with mvc3. I'm new to the whole .net
Since people are often using array.length < 1 to check if an array is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.