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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:24:16+00:00 2026-06-03T07:24:16+00:00

I want to DRY my source code. Let’s say I had some of these

  • 0

I want to DRY my source code. Let’s say I had some of these functions which mainly differ in one line – how could I achieve that??

public Result foo (int x, int y, int z){
    Log.Say("Foo started!");                                 <<< DIFFERENCE
    DoSomeMagic();
    try {
        result = controller.foo(x, y, z);                    <<< DIFFERENCE
    } catch (Exception){
        Log.Say("Something went terribly wrong");
    }
    return result;
}


public Result bar (Person a, string whatever){
    Log.Say("Bar started!");                                 <<< DIFFERENCE
    DoSomeMagic();
    try {
        result = controller.bar(a, whatever);                <<< DIFFERENCE
    } catch (Exception) {
        Log.Say("Something went terribly wrong");
    }
    return result;
}

It’s driving me nuts, because it can’t be that difficult? I’m too confused by the various approaches by now; so far I tried to do it with a delegates,
Func, Lambda expressions, anonymous functions, but I just can’t get it to work (guess I need a break).

public Result handler (Func callbackMethodWithArgs) {
    Result result = null;
    Log.Say(method + " started!");
    DoSomeMagic();
    try {
        result = invoke(callbackMethodWithArgs) as Result;
    } catch (Exception) {
        Log.Say("Something went terribly wrong");
    }
    return result;
}

public Result foo (int x, int y, int z) {
    return handler(controller.foo(x, y, z));
}

public Result bar (Person a, string whatever) {
    return handler(controller.bar(a, whatever);
}

As an addition it would be really great to have the possibility to use an anonymous function, like

public Result foo (int x, int y, int z) {
    return handler(delegate () {
        controller.foo(x, y, z));
        doSomethingOther();
    });
}

Any suggestions?? Thanks! (I read many question on similiar topics, but I didn’t find anything what solved the problem for me – so I assume it’s probably a duplicate – if so, I’m sorry)

  • 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-06-03T07:24:17+00:00Added an answer on June 3, 2026 at 7:24 am

    You can do this using Func<Result>:

    public Result handler (string name, Func<Result> method) {
        Result result = null;
        Log.Say(name + " started!");
        DoSomeMagic();
        try {
            result = method();
        } catch (Exception) {
            Log.Say("Something went terribly wrong");
        }
        return result;
    }
    

    And then at the call sites turn them into delegates:

    public Result foo (int x, int y, int z) {
        return handler("Foo", () => controller.foo(x, y, z));
    }
    
    public Result bar (Person a, string whatever) {
        return handler("Bar", () => controller.bar(a, whatever);
    }
    

    Note: If you’re on .NET 2.0 you can create the Func<_> delegate manually before using the code above:

    public delegate TResult Func<TResult>();
    

    As an aside, hiding exceptions in this way is not a great idea. You’re returning null and just logging an exception, as opposed to rethrowing it and letting calling code decide what to do. Could work for your use case, but is generally a red flag.

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

Sidebar

Related Questions

I want DRY/reuse as much editor code (View and Model) as possible. Some of
i'm trying to follow DRY and i've got some functions i have to reuse.
Here is a snippet of my JavaScript Code that I want to DRY up:
I need to convert between these two classes, and want to maintain DRY but
When I want to apply the DRY principle, i.e. to unify the code of
Want to use signal when follow() function called in UserProfile. I've written signal which
want to have a Hyperlink-Button in a gridView in which I can display a
I am trying to DRY up my code a bit so I am writing
I am trying to DRY up a bunch of forms code that has a
I want to test a patch using --dry-run , but when it fails, I

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.