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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:18:59+00:00 2026-06-04T21:18:59+00:00

Is there any way in C# to save a function call for later invoking?

  • 0

Is there any way in C# to save a function call for later invoking? For example, I want to be able to say:

public class MyFunctionCaller
{
    public static void SaveFunctionCallForLater(/* Some parameters*/)
    {
    // Save the Function Call in a variable or a list to be invoked after a set amount of time
   }
}

public class MyProgram
{
    public static void Main()
    {
        // Save a call to 'SpecialFunction' along with its parameters,
        // and maybe the object that's calling it (not applicable in
       // this example context, but important to my question)
        MyFunctionCaller.SaveFunctionCallForLater(/* Stuff */);
    }

    public void SpecialFunction(/* Stuff */)
    {
        // Does some cool stuff
    }
}

Let me give you some context: I’m creating a game in XNA and I want to create a DelayedFunctionCaller class, that any object can reference to hand complete function calls to, along with an amount of time to wait before it calls the function. I handle all of the time-waiting and triggering myself so that’s not where I’m lost, I’m just not sure how or the correct way to package up the function to be passed to the DelayedFunctionCaller.

Here’s the kicker: The DelayedFunctionCaller has to be able to run any function, no matter what object is sending it a function, what the function returns, or what parameters it takes**. I have over 100 classes in my game already, and the goal is to create an object that can save any function call from any one of them for later calling.

So from my own research, I’ve found the Type class, and I know that I can save the type of the object calling, the function name as a string (which is an acceptable sacrifice), then use GetMethod() to save the method in a MemberInfo, then use MemberInfo‘s Invoke() to call the function, even on a saved object, and with the parameters (as an array of Objects).

But this all seems very… hack-ish. I tried to look this up once and thought that Generic Delegates was the way I wanted to go, but I got so lost in there that I gave up. Am I on the right track? Is this the only way to do this? What about Generic Delegates, or was I just out of my mind when I thought it would solve my problem?

Any and all help is appreciated. Thank you!

  • 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-04T21:19:01+00:00Added an answer on June 4, 2026 at 9:19 pm

    You could save it as an Action, using a lambda expression:

    Action functionToCallLater = () => SpecialFunction(/* params */);
    // later
    functionToCallLater(); // calls SpecialFunction with its parameters
    

    If the function returns something and you need its value, simply use the Func<T> type instead.

    int MyMethod(int param) { return param + 1; }
    (...)
    Func<int> functionToCallLater = () => MyMethod(3);
    (...)
    int result = functionToCallLater(); // result == 4
    

    Keep in mind that any variable you use within the lambda is captured itself, not its value; if its value changes, that’ll be the value used when the function is called. See Eric Lippert‘s blog for a deeper explanation.

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

Sidebar

Related Questions

Is there any way to save my code folding in eclipse? It's horrible to
Is there any way to save an array of JSON object to a mongodb
Is there any way to save data obtained during app-run and afterwards use it
Is there any way to save session of crawler in mysql database or in
Is there any way, to save formatted in database from richtextbox? I've got richtextbox
Is there any way I can run class files (i.e. with main as the
Is there a way in Python to silence stdout without wrapping a function call
Is there any way on manually doing the create function? I have an scaffold,
Is there any way to change the BackColor of the border of a panel
Is there any way to view the reduction steps in haskell, i.e trace the

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.