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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:36:51+00:00 2026-05-20T08:36:51+00:00

I have an extension method that looks like the following: //[MethodImpl(MethodImplOptions.NoOptimization)] public static IEnumerable<char>

  • 0

I have an extension method that looks like the following:

//[MethodImpl(MethodImplOptions.NoOptimization)]
public static IEnumerable<char> TakeWhile(this BinaryReader reader, Func<int, bool> condition)
{
    while (condition(reader.PeekChar()))
    {
        char c = reader.ReadChar();
        yield return c;
    }
}

I use this method when parsing a file with a BinaryReader to skip a block of whitespace characters, amongst other things. What i have found is that the JIT compiler is optimising it out, when i call it like so:

// Skip white space
this.reader.TakeWhile(IsWhiteSpace);//.FirstOrDefault();

I have tried adding the [MethodImpl(…)] attribute to instruct the JIT compiler not to optimise out the method, but it does not work. Now obviously i could write another implementation of this which manipulates the underlying stream buffer position but out of curiosity i would like to know why this is so.

The only ways i have found to prevent the optimisation is to use the IEnumerable results (eg – via a call to .FirstOrDefault() as commented above) or copy the code into the calling method. I have tried preventing optimisation of the calling methods, using the MethodImplAttribute, but this does not work. Strangely, optimisation is turned off completely under the Debug build so it shouldn’t be occuring in any situation. Does anyone know another way to prevent the optimisation?

  • 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-20T08:36:52+00:00Added an answer on May 20, 2026 at 8:36 am

    No, the JIT isn’t optimizing it out. However, none of your code will be executed – because you’re ignoring the returned value. Until the first call to MoveNext(), none of the code in the iterator block is run. This has nothing to do with the JIT, and everything to do with how iterator blocks work.

    You may want to read my articles on iterator blocks (basics, implementation details) and Eric Lippert’s “psychic debugging” blog post (part 1; part 2).

    Note that calling FirstOrDefault() will only read the first character. It sounds like you really want to consume the whole stream until the condition fails – which means using something like Count() which will iterate over the entire returned sequence.

    Alternatively – and preferrably IMO – write an “active” method with a void return type to do this. If you’re not interested in the return value of a method, that’s a signal that it’s probably not an ideal method to call. (That’s not always the case, but it usually is.)

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

Sidebar

Related Questions

I would like to create a safe sum extension method that would have the
Does Python have extension methods like C#? Is it possible to call a method
I have an extension method on the HttpApplicationState object for getting my IoC container
I have a simple extension method on the int type so I can do
I have used extension methods to extend html helpers to make an RSS repeater:
So, I have a few extension methods, for commonly used stuff, and in documenting
I have a series of Extension methods to help with null-checking on IDataRecord objects,
I have a few 'helper' style extension methods I use quite regularly now (they
I have a class library with some extension methods written in C# and an
What are extension methods in .NET? EDIT: I have posted a follow up question

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.