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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:08:02+00:00 2026-05-11T02:08:02+00:00

I often find myself writing a property that is evaluated lazily. Something like: if

  • 0

I often find myself writing a property that is evaluated lazily. Something like:

if (backingField == null)    backingField = SomeOperation(); return backingField; 

It is not much code, but it does get repeated a lot if you have a lot of properties.

I am thinking about defining a class called LazyProperty:

public class LazyProperty<T>     {     private readonly Func<T> getter;      public LazyProperty(Func<T> getter)     {         this.getter = getter;     }      private bool loaded = false;     private T propertyValue;      public T Value     {         get         {             if (!loaded)             {                 propertyValue = getter();                 loaded = true;             }             return propertyValue;         }     }      public static implicit operator T(LazyProperty<T> rhs)     {         return rhs.Value;     } } 

This would enable me to initialize a field like this:

first = new LazyProperty<HeavyObject>(() => new HeavyObject { MyProperty = Value }); 

And then the body of the property could be reduced to:

public HeavyObject First { get { return first; } } 

This would be used by most of the company, since it would go into a common class library shared by most of our products.

I cannot decide whether this is a good idea or not. I think the solutions has some pros, like:

  • Less code
  • Prettier code

On the downside, it would be harder to look at the code and determine exactly what happens – especially if a developer is not familiar with the LazyProperty class.

What do you think ? Is this a good idea or should I abandon it ? Also, is the implicit operator a good idea, or would you prefer to use the Value property explicitly if you should be using this class ?

Opinions and suggestions are welcomed 🙂

  • 1 1 Answer
  • 1 View
  • 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-11T02:08:03+00:00Added an answer on May 11, 2026 at 2:08 am

    Just to be overly pedantic:

    Your proposed solution to avoid repeating code:

    private LazyProperty<HeavyObject> first =    new LazyProperty<HeavyObject>(() => new HeavyObject { MyProperty = Value }); public HeavyObject First {    get {      return first;    }  } 

    Is actually more characters than the code that you did not want to repeat:

    private HeavyObject first; public HeavyObject First {    get {     if (first == null) first = new HeavyObject { MyProperty = Value };     return first;   } } 

    Apart from that, I think that the implicit cast made the code very hard to understand. I would not have guessed that a method that simply returns first, actually end up creating a HeavyObject. I would at least have dropped the implicit conversion and returned first.Value from the property.

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

Sidebar

Related Questions

Often I find myself writing code like this: if (Session != null) { Session.KillAllProcesses();
Often when I'm using Python I'll find myself writing list comprehensions that look something
I often find myself writing R scripts that generate a lot of output. I
Using ValueInjecter, I often find myself writing code like this: var foo1 = new
In my day-to-day work, I often find myself writing classes like in this simplified
I often find myself writing class constructors like this: class foo: def __init__(self, arg1,
Quite often I find myself writing code like this: Dim oRenewalOrder = (From c
I often find myself writing small (5-20 lines) files for things like input validation,
When I'm programming, I often find myself writing functions that -should- (to be proper
I find myself writing stuff like this too often and it seems too wordy:

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.