CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: yield

  1. #1
    Join Date
    Jul 2007
    Posts
    273

    yield

    Hello,
    I was wonder if there's any chance to use the recursion in a mathod with yield instruction within a foeach, like this:
    Code:
    public IEnumerable<string> compute(string query) {
            foreach (string s in myObjs) {
              
                 .......................................
                 compute(query++)
                 if (...) yield return  query;
            }
    
    }
    I don't know where's the problem but not recursion happen at all

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: yield

    Quote Originally Posted by mickey0 View Post
    Hello,
    I was wonder if there's any chance to use the recursion in a mathod with yield instruction within a foeach, like this:
    Code:
    public IEnumerable<string> compute(string query) {
            foreach (string s in myObjs) {
              
                 .......................................
                 compute(query++)
                 if (...) yield return  query;
            }
    
    }
    I don't know where's the problem but not recursion happen at all
    Because yield has nothing to do with recursion. Recursion occurs when a method calls itself *recursively*. This is not a common thing to do in C# as it does not support tail recursion, so you may well blow your stack.

    Also, your code makes absolutely no sense (incrementing a string???) and would not even compile. You should spend some time learning the basics before messing around with iterators and recursive functions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured