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 a class (Foo) which lazy loads a property named (Bar). What is
I have a Lazy<List<T>> where T is a class which has a huge string
I have class A: public class ClassA<T> Class B derives from A: public class
If I have class ObjA { public ObjB B; } class ObjB { public
Suppose you have class B with lazily loaded property c . And that this
I have created a class in which I have set some of it's fields
I have a class product with child collection prices: public class Product { private
Technology: Hibernate 3.0 Suppose i have Entity class Company @Entity @Table(name=tbl_companies) public class Company
I have an ItemEntity class, which has a collection of ImageEntity's. I want to
I have made an openGL camera class that uses lazy evaluation to provide the

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.