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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:13:55+00:00 2026-05-27T20:13:55+00:00

I know it’s impossible to use return and yield return in the same method.

  • 0

I know it’s impossible to use return and yield return in the same method.

This is the code that I would like to optimize:

public IEnumerable<TItem> GetItems(int data)
{
    if (this.isSingleSet)
    {
        return this.singleSet; // is IEnumerable per-se
    }
    else
    {
        int index = this.GetSet(data);
        foreach(TKey key in this.keySets[index])
        {
            yield return this.items[key];
        }
    }
}

Important: I know this code doesn’t compile. It’s the code I have to optimize.

There are two ways that I know of that would make this method working:

  1. convert yield return part:

    ...
    else
    {
        int index = this.GetSet(data);
        return this.keySets[index].Select(key => this.items[key]);
    }
    
  2. convert return part:

    if (this.isSingleSet)
    {
        foreach(TItem item in this.singleSet)
        {
            yield return item;
        }
    }
    else ...
    

But there’s a big speed difference between the two. Using only return statements (in other words using Select()) is much much slower (like 6 times slower) to yield return conversion.

Question

Is there any other way that comes to your mind how to write this method? Do you have any other suggestions information that would be valuable to performance discrepancy?

Additional info

I was measuring speed of the two methods by using stopwatch around a for loop.

Stopwatch s = new Stopwatch();
s.Start();
for(int i = 0; i < 1000000; i++)
{
    GetItems(GetRandomData()).ToList();
}
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);

Each of these were loops were run in separate processes so there was could be no performance influence by garbage collection or anything else.

  1. I’ve run the program with one method version then
  2. Closed it
  3. Rewrote the method and run it again.

Did this few times to see reliable performance difference…

  • 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-27T20:13:56+00:00Added an answer on May 27, 2026 at 8:13 pm

    Use two functions. The outer, called by clients, function does all the non-lazy bits (like parameter validation) that you don’t want to delay. The private worker does the lazy bits:

    public IEnumerable<TItem> GetItems(int data) {
      if (this.isSingleSet) {
        return this.singleSet; // is IEnumerable per-se
      } else {
        return DoGetItems(data);
      }
    }
    
    private IEnumerable<TItem> DoGetItems(int data) {
      int index = this.GetSet(data);
      foreach(TKey key in this.keySets[index]) {
        yield return this.items[key];
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I can do something like $int = (int)99; //(int) has a
I know that this is a very commonly asked question, but all of the
I know some people use ANT to accomplish this, but I don't want to
Know of a way to mock %[]? I'm writing tests for code that makes
I know that this may be common knowledge, but is there a way to
I know that I can use ProxyPass to serve up my yardoc server instance
know nothing about php, but I have this script that reads a folder and
I know you can minify/condense Javascript or CSS code, but can you do this
Anyone know what type of JSON (if even that!) the following code is? I'm
I know the input have the maxlength, but I would like to have minlength

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.