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 8319401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:18:57+00:00 2026-06-08T22:18:57+00:00

To clarify I’m doing this in Unity3D, which may or may not be important?

  • 0

To clarify I’m doing this in Unity3D, which may or may not be important?

I’m trying to figure out if I can pass a value by ref to an IEnumerator function that does not yield. If I try to do it with one that yields, VS2010 complains (“Iterators cannot have ref or out parameters”), but, if I wrap the call up with a similar IEnumerator function that calls the yielding function, but does not yield itself, the error goes away and things appear to work. I’m trying to find out if I’m in unexpected behavior land or if this is normal behavior.

Here’s an example of what I’m doing:

IEnumerator Wrapper(ref int value)
{
    int tmp = ++value;    // This is the ONLY place I want the value
    return Foo(tmp);      // of the ref parameter to change!  
}                         // I do _NOT_ want the value of the ref
                          // parameter to change in Foo()!
IENumerator Foo(int value)
{
    // blah blah
    someFunc(value);
    someSlowFunc();
    yield return null;
    yield return null;
}
  • 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-06-08T22:19:00+00:00Added an answer on June 8, 2026 at 10:19 pm

    Looks good. The top function just returns an IEnumerator – but is otherwise a normal function. The bottom function is an IEnumerator [transformed into a funky class by the compiler] and as such cannot have a ref value.

    The top function could have been written as such:

     void Wrapper(ref int value, out IEnumerator coroutine)
     {
         int tmp = ++value;
         coroutine = Foo(tmp);
     }
    

    This is a little more messy – but it shows how this is a normal function that deals with two pieces of data. A int passed by referance, and a IEnumerator [just a class] that it returns [in this example by using out].


    Supplemental: This is how stuff works behind the scenes:

        static void Main(string[] args)
        {
            //Lets get the 'IEnumerable Class' that RandomNum gets compiled down into.
            var IEnumeratorClass = RandomNum(10, 10);
    
            //All an IEnumerable is is a class with 'GetEnumerator'... so lets get it!
            var IEnumerableClass = IEnumeratorClass.GetEnumerator();
    
            //It can be used like so:
            while (IEnumerableClass.MoveNext())
            {
                Console.WriteLine(IEnumerableClass.Current);
            }
    
            Console.WriteLine(new String('-', 10));
    
            //Of course, that's a lot of code for a simple job.
            //Luckily - there's some nice built in functionality to make use of this.
            //This is the same as above, but much shorter
            foreach (var random in RandomNum(10, 10)) Console.WriteLine(random);
    
            Console.WriteLine(new String('-', 10));
    
            //These simple concepts are behind Unity3D coroutines, and Linq [which uses chaining extensively]
            Enumerable.Range(0, 100).Where(x => x % 2 == 0).Take(5).ToList().ForEach(Console.WriteLine);
    
            Console.ReadLine();
        }
    
        static Random rnd = new Random();
        static IEnumerable<int> RandomNum(int max, int count)
        {
            for (int i = 0; i < count; i++) yield return rnd.Next(i);
        }
    
        //This is an example of what the compiler generates for RandomNum, see how boring it is?
        public class RandomNumIEnumerableCompiled : IEnumerable<int>
        {
            int max, count;
            Random _rnd;
            public RandomNumIEnumerableCompiled(int max, int count)
            {
                this.max = max;
                this.count = count;
                _rnd = rnd;
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return new RandomNumIEnumeratorCompiled(max, count, rnd);
            }
    
            IEnumerator<int> IEnumerable<int>.GetEnumerator()
            {
                return new RandomNumIEnumeratorCompiled(max, count, rnd);
            }
    
        }
        public class RandomNumIEnumeratorCompiled : IEnumerator<int>
        {
            int max, count;
            Random _rnd;
            int current;
            int currentCount = 0;
            public RandomNumIEnumeratorCompiled(int max, int count, Random rnd)
            {
                this.max = max;
                this.count = count;
                _rnd = rnd;
            }
    
            int IEnumerator<int>.Current { get { return current; } }
    
            object IEnumerator.Current { get { return current; } }
    
            public bool MoveNext()
            {
                if (currentCount < count)
                {
                    currentCount++;
                    current = rnd.Next(max);
                    return true;
                }
                return false;
            }
    
            public void Reset() { currentCount = 0; }
            public void Dispose() { }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to clarify one thing. This is not a question on which
To clarify before I begin, this is NOT homework but rather I am studying
To clarify: no I can't make this pure PHP5 yes this code works in
Can someone clarify that the role of a ClassLoader is not only to load
Just to clarify something first. I am not trying to convert a byte array
Lets clarify my question, I want to make an element which the element contains
To clarify, I currently have a live app that does not implement in app
Can some one clarify the space complexity of Node Relationship and property in the
I would like to clarify this issue. I had installed Cocos2d 1.01 rc on
Could you please clarify this part of Apple's documentation: Transitioning to ARC Release Notes

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.