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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:15:43+00:00 2026-05-28T05:15:43+00:00

Looks like I’ve missed something obvious but really can’t figure out why I can’t

  • 0

Looks like I’ve missed something obvious but really can’t figure out why I can’t use/access a method which was assigned to a passed in Func<> parameter, I want to access/call it in the external calling context.

Below the simple example: (real results data type is not string but some complex type)

class Caller
{
    private Func<string> factory;

    public Caller()
    {
        this.factory = null;
        var adapter = new Adapter(this.factory);
        adapter.Adapt(...);

        // then I would share this factory via IoC
        dependencyContainer.RegisterUsingFactoryMethod<string>(this.factory);

        // !!! but here is factory still not assigned
        string results = this.factory();
    }
}

class Adapter
{
    private string results;

    public Adapter(Func<string> factory)
    {
        factory = () => this.results;
    }

    public IOut Adapt(IIn input)
    {
       this.results = someCalculatedData;
    }
}
  • 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-28T05:15:44+00:00Added an answer on May 28, 2026 at 5:15 am

    This has nothing to do with delegates, and everything to do with the fact that all arguments are passed by value by default. You’d see the same behaviour if factory were an int, or a string, or a StringBuilder.

    If you want your Service constructor to modify the value of the argument, you need a ref or out parameter. However, I would personally suggest that you change it to use a method which returns a Func<string>:

    public class Caller
    {
        private readonly Func<string> factory;
    
        public Caller()
        {
            this.factory = Service.GetFunc();
        }
    }
    
    public class Service
    {
        public static Func<string> GetFunc()
        {
            return () => Guid.NewGuid().ToString();
        }
    }
    

    (There are various other options here, such as a method group conversion, or a property. It’s hard to know what to recommend when we don’t have much context.)

    Obviously you’d want to rename the method. But calling a constructor solely to modify an out parameter would be a very odd bit of code.

    For more on parameter passing, see my article on the topic.

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

Sidebar

Related Questions

Looks like its related to NSURLConnection, but not sure how I can diagnose? Exception
Looks like a silly question but I am really stuck and any help would
Looks like DUNIT is already integrated under Delphi XE. But I can't find it.
Looks like others have had this problem but I can't seem to find a
:vimgrep looks like a really useful thing. Here's how to use it: :vim[grep][!] /{pattern}/[g][j]
Looks like this question has been asked thousand times already, but each person's configuration
Looks like the options are Propel, Doctrine, and ActiveRecord, others?, but I am only
Looks like a dumb question, but I tried the following (where Me is a
It looks like this block directly flushes the output. What's the practical use of
Looks like count(*) is slower than NUM_ROWS. Can experts in this area throw some

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.