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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:22:57+00:00 2026-05-26T20:22:57+00:00

When designing a class that has a reference to another object it might be

  • 0

When designing a class that has a reference to another object it might be beneficial to only create the referenced object the first time it is used, e.g. use lazy loading.

I often use this pattern to create a lazy loaded property:

Encoding utf8NoBomEncoding;

Encoding Utf8NoBomEncoding {
  get {
    return this.utf8NoBomEncoding ?? 
      (this.utf8NoBomEncoding = new UTF8Encoding(false));
  }
}

Then I came across this code when browsing the source code for the BCL:

Encoding Utf8NoBomEncoding {
  get {
    if (this.utf8NoBomEncoding == null) {
      var encoding = new UTF8Encoding(false);
      Thread.MemoryBarrier();
      this.utf8NoBomEncoding = encoding;
    }
    return this.utf8NoBomEncoding;
  }
}

As far as I can tell none of these are thread safe. E.g. multiple Encoding objects may be created. I completely get that and know that it isn’t a problem if an extra Encoding object is created. It is immutable and will soon be garbage collection.

However, I’m really curious to understand why Thread.MemoryBarrier is necessary and how the second implementation is different from the first in multi-threading scenarios.

Obviously, if thread safety is a concern the best implementation is probably to use Lazy<T>:

Lazy<Encoding> lazyUtf8NoBomEncoding = 
  new Lazy<Encoding>(() => new UTF8Encoding(false));

Encoding Utf8NoBomEncoding {
  get {
    return this.lazyUtf8NoBomEncoding.Value;
  }
}
  • 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-26T20:22:58+00:00Added an answer on May 26, 2026 at 8:22 pm

    This code would be a disaster without the memory barrier. Look closely at these lines of code.

      var encoding = new UTF8Encoding(false);
      Thread.MemoryBarrier();
      this.utf8NoBomEncoding = encoding;
    

    Now, imagine some other thread sees the effects of the last line but doesn’t see the effects of the first line. That would be a complete disaster.

    The memory barrier ensures that any thread that sees encoding at all also sees all the effects of its constructor.

    For example, without the memory barrier, the first and last lines could be internally optimized (roughly) as follows:
    1) Allocate some memory, store a pointer to it in this.utf8NoBomEncoding
    2) Call the UTF8Encoding constructor to fill in that memory with valid values.

    Imagine if between steps 1 and 2 another thread runs and passes through this code. It will use an object that has not been constructed yet.

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

Sidebar

Related Questions

What advice/suggestions/guidance would you provide for designing a class that has upwards of 100
Suppose I am designing a class that can handle any database technology to create
I'm designing a class that has two dependencies. One of the dependency classes has
I am designing an entity class which has a field named documentYear, which might
I am currently designing a class library that will provide data to a web
I'm wondering how you'd recommend designing a class, given the fact that XNA Framework
I have got a designing dilema. I have a class named UserAccount that represent
We're designing an Android app that has several activities which are working in a
I'm designing an iOS static library that will be used by other developers. This
I'm designing an application that has two [three including administrators] user types: Buyers and

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.