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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:53:52+00:00 2026-05-10T20:53:52+00:00

Update for future readers: When .NET 4 comes out, LazyInit<T> from the CTP will

  • 0

Update for future readers: When .NET 4 comes out, LazyInit<T> from the CTP will have been renamed to Lazy<T> and will be changed from a struct to a class, so very little of this will apply, except as an illustration of why mutable structs can be problematic if you’re not careful.

I’ve been experimenting with LazyInit in the Parallel Extensions June CTP, and I would expect the following code to print out the same Guid a thousand times, but instead it prints out a thousand different Guids. Clearly I’m missing something obvious here about how LazyInit is supposed to work, and I’d appreciate if someone would kindly point out what it is.

using System; using System.Diagnostics; using System.Threading;  namespace TestApp {     class Program     {         static void Main(string[] args)         {             for (int i=0; i < 1000; i++)             {                 Console.WriteLine(TestClass.Instance.Id);             }              Console.Write('Press any key to continue:');             Console.ReadKey();         }          private class TestClass         {             private static readonly LazyInit<TestClass> _instance = new LazyInit<TestClass>(() => new TestClass(), LazyInitMode.EnsureSingleExecution);              public static TestClass Instance             {                 get { return _instance.Value; }             }              private TestClass()             {                 Debug.WriteLine('TestClass Constructor');                 Id = Guid.NewGuid();             }              public Guid Id { get; private set; }         }     } } 
  • 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. 2026-05-10T20:53:53+00:00Added an answer on May 10, 2026 at 8:53 pm

    Short Version: Make the static non-readonly and it will fix the bug you are experiencing.

    Long Version: This is a very misunderstood portion of C#. When you access a struct you are accessing a copy of the struct. The underlying call of LazyInit.Value is a mutating operation. Normally a copyback is performed but in the case of a read-only field there is no way to perform the copy back and hence you are still left with an uninitialized value.

    Extremely detailed explanation: http://ericlippert.com/2008/05/14/mutating-readonly-structs/

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

Sidebar

Related Questions

I have been laying out a GUI uing ASP.NET and find I am confused
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
UPDATE : from what I'm hearing, I was imagining the problem I describe below.
From what I have understood there is a big difference between the Html.RenderPartial included
(Note to any future readers: The error, unsurprisingly, is in my code and not
From the Wikipedia article on Read-Copy-Update : The reason that it is safe to
Update: this question has been answered (see below). I'll leave it up in case
Update : My bad...I have an primary key on those tables..I meant no further
I'd like to create/update a sub-object within an admin edit dialog. I have a
Is there a way to update a side branch with information from another (master

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.