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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:37:15+00:00 2026-05-13T00:37:15+00:00

I have class Lazy which lazily evaluates an expression: public sealed class Lazy<T> {

  • 0

I have class Lazy which lazily evaluates an expression:

public sealed class Lazy<T>
{
    Func<T> getValue;
    T value;

    public Lazy(Func<T> f)
    {
        getValue = () =>
            {
                lock (getValue)
                {
                    value = f();
                    getValue = () => value;
                }
                return value;
            };
    }

    public T Force()
    {
        return getValue();
    }
}

Basically, I’m trying to avoid the overhead of locking objects after they’ve been evaluated, so I replace getValue with another function on invocation.

It apparently works in my testing, but I have no way of knowing if it’ll blow up in production.

Is my class threadsafe? If not, what can be done to guarantee thread safety?

  • 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-13T00:37:15+00:00Added an answer on May 13, 2026 at 12:37 am

    Can’t you just omit re-evaluating the function completely by either using a flag or a guard value for the real value? I.e.:

    public sealed class Lazy<T>
    {
        Func<T> f;
        T value;
        volatile bool computed = false;
        void GetValue() { lock(LockObject) { value = f();  computed = true; } }
    
        public Lazy(Func<T> f)
        {
            this.f = f;
        }
    
        public T Force()
        {
            if (!computed) GetValue();
            return value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a class which has many virtual properties lazy loaded public class
My Moo based class has both lazy & non-lazy attributes which have both default
I have a Lazy<List<T>> where T is a class which has a huge string
I have a class (Foo) which lazy loads a property named (Bar). What is
I have this code: class LFSeq: # lazy infinite sequence with new elements from
I tried the following in Clojure, expecting to have the class of a non-lazy
I have Class and Student objects. Both have collection of another as property. Which
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have class with back reference: public class Employee : Entity { private string
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private

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.