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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:13:23+00:00 2026-05-31T20:13:23+00:00

I have a situation where I need to generate a few similar anonymous delegates.

  • 0

I have a situation where I need to generate a few similar anonymous delegates. Here’s an example:

public void Foo(AnotherType theObj)
{
    var shared = (SomeType)null;

    theObj.LoadThing += () =>
    {
        if(shared == null)
            shared = LoadShared();

        return shared.Thing;
    };

    theObj.LoadOtherThing += () =>
    {
        if(shared == null)
            shared = LoadShared();

        return shared.OtherThing;
    };

    // more event handlers here...
}

The trouble I’m having is that my code isn’t very DRY. The contents of each of the event handlers is EXTREMELY similar, and could be easily parameterized into a factory method. The only thing preventing me from doing that is that each delegate needs to share the reference to the shared variable. I can’t pass shared to a factory method with the ref keyword, as you can’t create a closure around a ref varaiable. Any ideas?

  • 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-31T20:13:24+00:00Added an answer on May 31, 2026 at 8:13 pm

    There’s no problem that can’t be solved by adding more abstraction. (*)

    The pattern you are repeating over and over again is the “lazy loading” pattern. That pattern is highly amenable to being captured in a type, and in fact, it has been, in version 4 of the framework. Documentation here:

    http://msdn.microsoft.com/en-us/library/dd642331.aspx

    You could then do something like:

    public void Foo(AnotherType theObj)
    {
        var shared = new Lazy<SomeType>(()=>LoadShared());
        theObj.LoadThing += () => shared.Value.Thing;
        theObj.LoadOtherThing += () => shared.Value.OtherThing;
        // more event handlers here...
    }
    

    And there you go. The first time shared.Value is accessed the value gets loaded; every subsequent time the cached value is used. Extra bonus: this is even threadsafe should the shared value be accessed on multiple threads. (See the documentation for details about precisely what guarantees we make regarding thread safety.)


    (*) Except of course for the problem “I have too much abstraction.”

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

Sidebar

Related Questions

I have a situation where I need to generate a bunch of C# code
I have a situation where I need to generate a table from a stored
Im using ITextSharp to generate a PDF, and have a situation where a need
I have a situation where I need to generate the below XML dynamically in
I have a situation where I have someFunction(int) , and I need to generate
I have situation where I need to change the order of the columns/adding new
I have a situation where I need to dynamically build up a list of
I have a situation where I need to pass two parameters to an action.
I have a situation where I need to update a control referenced in a
I have a situation where I need to notify some users when something in

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.