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 at the extjs4 documentation for the itemselector, theres a method getValue() that returns
Looking for an example that: Launches an EXE Waits for the EXE to finish.
Looking for a Linux application (or Firefox extension) that will allow me to scrape
Looking at the processmodel element in the Web.Config there are two attributes. maxWorkerThreads=25 maxIoThreads=25
Looking for a good rss/feed reader for windows or if there are any good
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?

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.