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

  • SEARCH
  • Home
  • 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 682879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:38:44+00:00 2026-05-14T01:38:44+00:00

When you write an iterator function in C#, using yield syntax, the compiler internally

  • 0

When you write an iterator function in C#, using yield syntax, the compiler internally translates your function into a class. So, if I write something like this:

IEnumerator<int> MyIterator () {
  for (int i = 0; i < 10; i++)
    yield return i;
}

Invoking MyIterator() constructs a class with a private field for i, instead of using a stack variable.

Now for the question: I want to find a way to locate the field used to store i’s value. The reason is a little involved:

I’m using iterator functions for cooperative threading. I write simple code like this in an iterator, in order to suspend execution of a function until an asynchronous operation has completed.

Future<string> f;
yield return file.readLine(out f);
string line = f.Result;

When those three statements are executed, the readLine function is invoked and stores a future into the ‘f’ field. The iterator function is then suspended, and wakes back up when the ‘f’ Future has had a result stored into it, at which point I can retrieve the result.

What I want to be able to do, is this:

string line;
yield return file.readLine(out line);

Under normal circumstances, this wouldn’t be possible in .NET, since there is no way to guarantee that a stack variable stays alive long enough for me to be able to write to it.

However, knowing that iterator functions store all their locals inside of fields, I can theoretically do this by holding a reference to the iterator (keeping it alive) and to the field itself, so that when the readLine operation completes, I can store the result directly into the field.

The problem is that the CLR doesn’t seem to give me a way to do this.

If I have an ‘out’ or ‘ref’ parameter, I can use unsafe C# or raw MSIL to convert that parameter into a raw pointer, a reference, or a TypedReference. Unfortunately, it’s not legal to store any of those three types in a field – even if you trick the compiler into letting you do it, the bytecode verifier won’t let you execute the code. This is most likely because storing a reference to a stack variable would be inherently unsafe.

So what I’m looking to do is write a function that takes an out/ref parameter, and searches the calling iterator’s fields to find a corresponding field. Once it has the field, it stores a reference to the iterator and field into a wrapper type and associates the wrapper with an asynchronous operation.

Given the overhead involved in runtime reflection in .NET, I suspect that this isn’t possible without either preprocessing my source code or post-processing my assemblies after compilation.

However, I’d love to hear some suggestions for alternate solutions. 🙂 Any ideas?

  • 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-14T01:38:44+00:00Added an answer on May 14, 2026 at 1:38 am

    I finally found a solution that meets my needs here, and what do you know – it involves LINQ.

    A blog post on .NET mentioned offhand how you can convert any expression lambda into an Expression<> object if you know the result type of the expression. Given that, it turns out that you can take an expression like this:

    () => this.Foo.Bar

    And walk through the nodes to end up with a MemberInfo (for ‘Bar’) and a ‘this’ object (for ‘this.Foo’). Given these two values, you can bind directly to a field or property specified at compile time. In the case of a property, you can do this very efficiently by retrieving the Get and Set methods of the property from the MemberInfo and converting them into strongly-typed delegates, making the cost of reading/writing that property very low.

    Best of all, this works perfectly with automated refactoring and source code searches because it’s putting the compiler’s muscle to use for you, and it doesn’t require any modifications to existing code in order to allow binding.

    The only downside is that it’s very difficult to bind to fields/properties of a struct, but I don’t consider that a particularly relevant use case anyway.

    Anyone looking to solve this or a similar problem can view the source code here:

    http://code.google.com/p/fracture/source/browse/trunk/Squared/Util/Bind.cs

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

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are several components available to accomplish this exact task… May 17, 2026 at 3:13 am
  • Editorial Team
    Editorial Team added an answer Looks like a virtual keyboard to me :-) Almost the… May 17, 2026 at 3:13 am
  • Editorial Team
    Editorial Team added an answer The tty input mode defaults to line input, so you… May 17, 2026 at 3:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Do you know a JavaScript library that implements a generic Iterator class for collections
I wanted to write a higher order function filter with C++. The code I
I'm trying to write a simple GUI front end for Plurk using pyplurk. I
I'm compiling using Code::Blocks on Windows 7 using the MinGW compiler (which I can
For my programming class I have to write a linked list class. One of
In a moment of madness, I decided to write a quadtree C++ template class.
I'm trying to write a function to print a representation of common STL containers
I tried to write a short function to invert an std::map<K, V> (I know
Someone told me that I can write for (iterator it = somecontainer.begin(); it !=
I'm new to Scala, and from what I understand yield in Scala is not

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.