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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:40:38+00:00 2026-05-23T10:40:38+00:00

I was refactoring some code which uses methods that have out parameters. I’m not

  • 0

I was refactoring some code which uses methods that have out parameters. I’m not particularly anti out parameters, but for what I’m doing I rarely need more than one of the values outed.

I have half a dozen methods with the same signature “void GetThings(T thing, out x, out y)”

so I wanted easy ways of saying “gotY = NewMethod(T thing)”

I came up with this. Questions to follow:

    [TestFixture]
public class TestsBase
{
    [Test]
    public void Test1()
    {
        Del.ImaExtMethod(2);
        Del2.ImanotherExtMethod(2);
    }

    public static MyDel Del = TestTest;
    public static MyDel2<int> Del2 = TestTest;

    public static void TestTest(int input, out int output)
    {
        output = input * 2;
    }
}

public delegate void MyDel(int input, out int output);
public delegate void MyDel2<T>(T input, out T output);

public static class TestExts
{
    public static int ImaExtMethod(this MyDel del, int input)
    {
        int result;
        del(input, out result);
        return result;
    }

    public static T ImanotherExtMethod<T>(this MyDel2<T> del, T input)
    {
        T result;
        del(input, out result);
        return result;
    }
}

Now I’m a bit torn. On the one hand it’s a reasonably elegant solution, on the other hand it’s not exactly intuitive if you haven’t seen this style before.

So before I rip it out again and do something else what are your thoughts? How would you tackle the above refactoring? My third option is to return a payload class with all the out parameters setting properties on it. It’s probably clearer than what I’ve done above, but feels less… fun?…

Yes, I’m the same person who keeps saying “Clarity trumps brevity” :/

  • 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-23T10:40:38+00:00Added an answer on May 23, 2026 at 10:40 am

    Personally, I think its clearer to do similar to what you have done, but to avoid the use of extension methods. This has the advantage that you don’t need to assign your method to an explicit delegate reference, and though its less fun, personally I think its easier to read:

    public delegate void SingleInDualOut<TIn, TOut, TOut2>(TIn input, out TOut out1, out TOut2 out2);
    
    public static class DelegateHelper
    {
        public static TOut FirstOutputParam<TIn, TOut, TOut2>(SingleInDualOut<TIn, TOut, TOut2> del, TIn input)
        {
            TOut out1;
            TOut2 out2;
    
            del(input, out out1, out out2);
    
            return out1;
        }
    
        public static TOut2 SecondOutputParam<TIn, TOut, TOut2>(SingleInDualOut<TIn, TOut, TOut2> del, TIn input)
        {
            TOut out1;
            TOut2 out2;
    
            del(input, out out1, out out2);
    
            return out2;
        }
    }
    
    public static class DelegateTest
    {
        public static void TestMethod(int input, out int output1, out int output2)
        {
            output1 = input * 2;
            output2 = input * 3;
        }
    }
    

    then call it like this:

    DelegateHelper.FirstOutputParam<int, int, int>(DelegateTest.TestMethod, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been refactoring throwaway code which I wrote some years ago in a
Whilst refactoring some code I came across some getter methods that returns a std::string.
I am currently refactoring some code which performs Windows Impersonation for testability and have
I'm refactoring some code and I have written a method that modifies a Dictionary
I have some code which is the same except that a certain sequence of
I have a large C++ code base that I'm doing some refactoring on where
I'm in the process of refactoring some code which includes moving folders around, and
We are in the process of refactoring some code. There is a feature that
Whilst refactoring some old code I realised that a particular header file was full
I have several files with code testing code (which uses a unittest class). Later

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.