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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:06:45+00:00 2026-05-31T21:06:45+00:00

I am trying to wrap my head around whether this is possible. My hunch

  • 0

I am trying to wrap my head around whether this is possible. My hunch is that it is not but wanted to confirm.

The following higher order function uses the null coalescing operator:

    public static Func<T> Coalesce<T>(this Func<T> source)
        where T : class
    {
        T local = default(T); 
        return delegate
        {
            return local ?? (local = source()); 
        }; 
    }

The goal is to use it inside a property like so:

    protected string SomeMember
    {
        get { return Coalesce(() => GetSomeMember())(); }
    }

If it worked as intended, the GetSomeMember() function would only be called once the first time the property is called. Thereafter the stored instance of the property can be returned. (its basic memoization / null coalescing concept).

Now the tricky part comes in trying to trap the stored instance inside a closure instead of opting to use a private field. I know you could store the state of ‘SomeMember’ inside the class which contains SomeMember, but I am explicitly trying to avoid that just for curiosity’s sake. The goal would be for everything needed to remain inside the get { } block (that includes not storing the delegate returned from Coalesce()).

Since both the inner returned function and the outer function are called everytime the property is accessed, there is a problem. The ‘T local’ variable gets re-assigned every time, and thus the null coalescing operator always re-invokes GetSomeMember().

Thoughts?

  • 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-31T21:06:46+00:00Added an answer on May 31, 2026 at 9:06 pm

    It wouldn’t work like this, because you’re calling Coalesce() (not a great name, BTW) every time you call the getter. You could save the delegate to a field and use that. But if you’re doing that, it’s much better to use Lazy<T> from the framework:

    private Lazy<string> m_someMember = new Lazy<string>(GetSomeMember);
    
    protected string SomeMember
    {
        get { return m_someMember.Value; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to wrap my head around this, but I seem to go in
Im trying to wrap my head around this but am spending too much time
Trying to wrap my head around the jQuery .not() function, and running into a
I'm trying to wrap my head around Arquillian and am just not fundamentally understanding
I am trying to wrap my head around serialization. But what happens if for
I am trying to wrap my head around javascript modules, but I'm unsure how
I'm trying to wrap my head around this and I think the easiest way
I'm trying to wrap my head around Object Oriented programming. But I'm having some
I'm trying to wrap my head around singletons and I understand that the App
I'm trying to wrap my head around Javascript's function.apply(). You use it like this:

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.