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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:48:42+00:00 2026-05-11T07:48:42+00:00

I have a class (Foo) which lazy loads a property named (Bar). What is

  • 0

I have a class (Foo) which lazy loads a property named (Bar). What is your preferred way to protect against mistaken use (due to intellisense or inexperienced staff) of the uninitialized backing field?

I can think of 3 options:

  class Foo {     // option 1 - Easy to use this.bar by mistake.      string bar;     string Bar {         get {             // logic to lazy load bar               return bar;          }     }      // option 2 - Harder to use this._bar by mistake. It is more obscure.     string _bar2;     string Bar2 {         get {             // logic to lazy load bar2               return _bar2;         }     }      //option 3 - Very hard to use the backing field by mistake.      class BackingFields {         public string bar;      }      BackingFields fields = new BackingFields();      string Bar3 {         get {             // logic to lazy load bar               return fields.bar;         }     }  } 

Keep in mind, the only place I want people mucking around with the backing field bar is in setter and getter of the property. Everywhere else in the class they should always use this.Bar

Update

I am currently using the following Lazy implementation (not for all properties with backing fields, but for select ones that require lazy loading, synchronization and notification). It could be extended to support futures as well (force evaluation in a separate thread in a later time)

Note My implementation locks on read, cause it supports an external set.

Also, I would like to mention that I think this is a language limitation which can be overcome in Ruby for example.

You can implement lazy in this way.

x = lazy do     puts '<<< Evaluating lazy value >>>'     'lazy value' end  puts x # <<< Evaluating lazy value >>> # lazy 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. 2026-05-11T07:48:43+00:00Added an answer on May 11, 2026 at 7:48 am

    Option 5

    Lazy<T>

    works quite nicely in several situations, though option 1 should really be just fine for most projects so long as the developers aren’t idiots.

    Adding [EditorBrowsable(EditorBrowsableState.Never)] to the field won’t help if it is private since this logic only kicks in for intellisense generated from metadata rather than the current code (current project and anything done via project references rather than dlls).

    Note: Lazy<T> is not thread safe (this is good, there’s no point locking if you don’t need to) if you require thread safety either use one of the thread safe ones from Joe Duffy or the Parallel Exetensions CTP

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

Sidebar

Related Questions

I have a class which is not thread safe: class Foo { /* Abstract
I have a base class Foo that has an Update() function, which I want
Let's say I have a class: class Foo { public string Bar { get
I have a class Foo which has several methods like button_0_0 , button_0_1 ,
I have a class Foo which overrides equals() and hashCode() properly. I would like
Let's say I have the following class: class Foo { public: Foo() { Bar();
Assume I have a class foo, and wish to use a std::map to store
I have this class. public class Foo { public Guid Id { get; set;
Let's say I have one class Foo that has a bunch of logic in
Let's say I have a class in my web app called class Foo. It

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.