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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:05:53+00:00 2026-06-08T08:05:53+00:00

I have a chunk of code that does a file put operation inside a

  • 0

I have a chunk of code that does a “file put” operation inside a moderately complicated piece of retry and try/catch logic. This is working fine, it looks roughly like this:

while (...) {
try {
    FilePut(source, destination)
    }
catch () {
   //Check exception type, possibly re-throw, possibly return, possibly increment
   //counters being checked by while loop
   }
}

The details of this logic aren’t the issue. But I’m realizing I have several other operations that also need to execute inside this same kind of logic and I want to avoid copy and pasting this logic around my application. I’d like to move it to a function and reuse that function. That function would have to take some sort of reference to the operation to be called, and the try logic would execute that operation (file put, file get, whatever).

This seems like a great place for a delegate, but the problem is, each of these operations has a different signature, so I’m not sure how to be able to write my above logic to be able to call “any” operation.

Is there a good way to do this in C#?

  • 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-08T08:05:55+00:00Added an answer on June 8, 2026 at 8:05 am

    You need an Action delegate to hide all of the different signatures. Something like this:

    public void DoAction(Action action)
    {
        // Make the boilerplate code whatever you need it to be. 
        // I've just used a try catch for simplicity.
        try
        {
            // Call the action at the appropriate time.
            action();
        }
        catch
        {
            // Handle any exceptions as you wish.
        }
    }
    

    Then, to be able to handle actions with different signatures, you can define a few overloads that take different types of the generic Action<T> delegates and all of the necessary arguments. These overloads will “curry” the generic action and its arguments to a plain Action:

    public void DoAction<T>(Action<T> action, T arg1)
    {
        DoAction(() => action(arg1));
    }
    
    public void DoAction<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2)
    {
        DoAction(() => action(arg1, arg2));
    }
    
    public void DoAction<T1, T2, T3>(Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3)
    {
        DoAction(() => action(arg1, arg2, arg3));
    }
    
    // etc...
    

    To use:

    public void SomeOtherMethod()
    {
        DoAction(MethodThatTakesTwoInts, 42, 23);
        DoAction(MethodThatTakesAString, "Don't Panic!");
    }
    

    Also, if you not familiar with them, take a look at the related family of Func delegates as well for good measure.

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

Sidebar

Related Questions

So I have a chunk of code that declares some classes, creates data, uses
I have a chunk of HTML code (with some PHP vars echoed inside of
I have a chunk of javascript that users can copy and paste to put
I have a rather large chunk of code that breaks sometimes, as far as
I have a chunk of PHP code that I'd like to include on a
If I have a chunk of code like this: .hover( function () { hoverState($(#navbar
I have got a chunk of code like for i in range(0, len(a)) b[i]
I have the following chunk of code. It works perfectly. <div id=restaurant_locations></div> <script type=text/javascript>
I have a chunk of C and C++ code. I have to release the
I came across the following weird chunk of code.Imagine you have the following typedef:

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.