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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:32:08+00:00 2026-05-27T22:32:08+00:00

I was looking at LazyInitializer.EnsureInitialized(ref T, Func{T}) in Reflector, and there appears to be

  • 0

I was looking at LazyInitializer.EnsureInitialized(ref T, Func{T}) in Reflector, and there appears to be a volatile local variable in that method volatile object local1 = s_barrier;
. I can think of two possible reasons for this:

  1. .NET may get to use features that are not supported by a given language, or

  2. The actual code does not declare a volatile local variable, but when the compiled code is decompiled by Reflector, it looks like a volatile local variable.

Does anyone know which is the case here (or whether there might be some other explanation)? If it is a matter of decompilation, does anyone have an idea of what the “true” code would look like?

  • 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-27T22:32:08+00:00Added an answer on May 27, 2026 at 10:32 pm

    This looks like a Reflector bug : it’s just a normal volatile read of the s_barrier field. There’s no “special” IL here that’s not expressible in C#.

     L_000d: volatile. 
     L_000f: ldsfld object modreq(System.Runtime.CompilerServices.IsVolatile) System.Threading.LazyInitializer::s_barrier
    

    This is just the normal code the compiler emits when reading from a static volatile field.


    Here’s a simpler repro: just compile the following (wrapped in a type) in release mode:

    private static volatile object field;
    
    private static void Main()
    {
        var temp = field;
    }
    

    Reflector produces the following decompiled C#:

    private static void Main()
    {
        volatile object field = Program.field;
    }
    

    when the IL is actually:

    L_0000: volatile. 
    L_0002: ldsfld object modreq([mscorlib]System.Runtime.CompilerServices.IsVolatile) WindowsFormsApplication1.Program::field
    L_0007: pop 
    L_0008: ret 
    

    UPDATE:
    Here’s my guess about what’s happening: In release mode, the C# compiler optimizes away the assignment of the value of the field (the result of the volatile read) to the local variable (the stloc instruction) since the local is not subsequently used. This appears to confuse Reflector. If you changed the method to use the subsequently use local, the stloc (or similar) instruction would indeed be emitted, following which the decompiled output from Reflector looks sensible.

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

Sidebar

Related Questions

Looking through the achievement list for Visual Studio 2010, it says that there is
Looking through some other SO questions, it appears that other people are having problems
Looking for a control that allows to select one text value at a time
Looking for a general case solution to determine if any jquery dialog (there are
Looking at all the examples of Operational Transformation Frameworks out there, they all seem
Looking for an equivalent container class that matches to C#'s Arraylist collections class. Is
Looking for simple php class routing, like this: http://fatfree.sourceforge.net/page/routing-engine Is there a ready-made solutions?
Looking at the list of annotations in FindBugs 2.0 , I see that a
Looking at the output of this particular method is confusing to me. I'm trying
Looking to place a button that triggers a simple SMS script on my Tropo

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.