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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:00+00:00 2026-05-28T04:51:00+00:00

How would I pass a list of values into a function, where the values

  • 0

How would I pass a list of values into a function, where the values are already stored in a list and the function isn’t the same every time?

To explain, I’ve got a list of several different types of custom objects (A List<object> to make this work) and I want to pass those objects into a function. However, the function isn’t always the same. I could have several different functions, and, assuming that List’s contents will always match the function’s input variables, I want to be able to pass the list’s contents into my function.

The following code is an example of what might work, but for one flaw:

List<object> myListOfVariables = new List<object>();
myListOfVariables.Add("Hello, world!");
myListOfVariables.Add(10);

void SayHelloXTimes(string helloString, int x) {
    for(int i = 0;i<x;i++) {
        print(helloString)
    }
}

SayHelloXTimes(myListOfVariables[0], myListOfVariables[1]);

Now, since I know my list will always contain the right amount of variables in the right positions, that would work, if I only had one function. But the problem is, I need to expand this so I could take apart my list and pass it into functions with different amounts of parameters.

For other reasons, I know my list will always have the right amount of variables in the right positions, so we don’t need to worry about that. I’ll also know the name of the function I need to pass my values into. I suppose I could do a load of if statements depending on the length of my list, like this:

if (myListOfVariables.Length == 2) { 
    SayHelloXTimes(myListOfVariables[0], myListOfVariables[1]); 
}

else if (myListOfVariables.Length == 3) {
    SayHelloXTimesForY(myListOfVariables[0], myListOfVariables[1], myListOfVariables[2]);
}

However, this (obviously) is really clunky code and I’d like to avoid it at all costs. Is there another solution to my problem? I know this is really confusing, but I did my best to explain it. If you’re still confused as to what I’m trying to do, please let me know.

And no, this is not a homework problem. 😉

  • 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-28T04:51:01+00:00Added an answer on May 28, 2026 at 4:51 am

    I think want you want to do can be done using reflection. Look at MethodBase.Invoke Method

    All you have to do is add all the parameters in the order the function expects to an object array.

        class Program
        {
    
            public static void SayHelloXTimes(string helloString, int x)
            {
                for (int i = 0; i < x; i++)
                {
                    Console.WriteLine(helloString);
                }
            }
    
            static void Main(string[] args)
            {
                MethodInfo Method = typeof(Program).GetMethod("SayHelloXTimes");
                Method.Invoke(null, new object[] { "foo", 3 });
    
                Console.ReadLine();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to pass values into the constructor on the class that implements
I would like to pass a reference of a method into another method and
I would like to pass the multiple arguments with positive or negative values. Is
I would like to pass to a jQuery function a regular function, instead of
I have a list of values I want to insert into a table via
I've got some data in a view that I would like to pass to
I want to pass a parameter into my function. The parameter will be a
In a WinForms UserControl, I would pass data to the main GUI thread by
My brain is fried, so I thought I would pass this one to the
I would like to pass an argument(s) to a method being defined using define_method,

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.