Is there a way to save a method in a variable then call it later? What if my methods return different types?

Related searches

Edit: Thank you for the answers. I am currently working on it!!\

I have 3 methods, S() returns string, D() returns double and B() returns bool.

I also have a variable that decides which method I use. I want do this:

    // I tried Func<object> method; but it says D() and B() don't return object.
    // Is there a way to use Delegate method; ? That gives me an eror saying method group is not type System.Delegate
    var method;

    var choice = "D";

    if(choice=="D")
    {
        method = D;
    }
    else if(choice=="B")
    {
        method = B;
    }
    else if(choice=="S")
    {
        method = S;
    }
    else return;

    DoSomething(method); // call another method using the method as a delegate.

    // or instead of calling another method, I want to do:
    for(int i = 0; i < 20; i++){
       SomeArray[i] = method();
    }

Is this possible?

I read this post: Storing a Method as a Member Variable of a Class in C# But I need to store methods with different return types...

Well, you could do:

Delegate method;

...
if (choice == "D") // Consider using a switch...
{
    method = (Func<double>) D;
}

Then DoSomething would be declared as just Delegate, which isn't terribly nice.

Another alternative would be to wrap the method in a delegate which just performs whatever conversion is required to get the return value as object:

Func<object> method;


...
if (choice == "D") // Consider using a switch...
{
    method = BuildMethod(D);
}

...

// Wrap an existing delegate in another one
static Func<object> BuildMethod<T>(Func<T> func)
{
    return () => func();
}

Returning a Value from a Method (The Java™ Tutorials > Learning , If this section confuses you, skip it and return to it after you have finished the lesson on interfaces and inheritance. When a method uses a class name as its return� Edit: Thank you for the answers. I am currently working on it!!\ I have 3 methods, S() returns string, D() returns double and B() returns bool. I also have a variable that decides which method I

private delegate int MyDelegate();
private MyDelegate method;


    var choice = "D";

    if(choice=="D")
    {
        method = D;
    }
    else if(choice=="B")
    {
        method = B;
    }
    else if(choice=="S")
    {
        method = S;
    }
    else return;

    DoSomething(method); 

Passing information between methods, You could pass these values to a method and then receive the result back. Let's start with how to give, or pass, a value to a method. Purpose: Example of program with two methods. Unlike before, the brackets that follow the method name now contain a variable declaration, in this case for a variable y which is of type� throws an exception (covered later), whichever occurs first. You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value. Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so.

Func<object> method;

var choice = "D";

if(choice=="D")
{
    method = () => (object)D;
}
else if(choice=="B")
{
    method = () => (object)B;
}
else if(choice=="S")
{
    method = () => (object)S;
}
else return;

DoSomething(method); // call another method using the method as a delegate.

// or instead of calling another method, I want to do:
for(int i = 0; i < 20; i++){
   SomeArray[i] = method();
}

4. Methods Use Instance Variables: How Objects Behave, In other words, methods use instance variable values. A variable with a type and a name, that can be used inside the body of the method. If you declare a method to return a value, you must return a value of the declared type! to one that protects your data and protects your right to modify your implementation later. Thus, hiding the variable number as private and then using a setter comes to the rescue. On the other hand, a getter method is the only way for the outside world to read the variable’s value

Writing Methods, If the method returns a value, it is as if the method call is "replaced" by the value that declare a local variable, decide how to initialize it, and then return that value. file (although we can and will later use one file to define multiple classes ). Methods can return a value to the caller. If the return type (the type listed before the method name) is not void, the method can return the value by using the return keyword. A statement with the return keyword followed by a variable, constant, or expression that matches the return type will return that value to the method caller.

Some Tips, Tricks, and Common Errors — How to Think Like a , There are two kinds of functions: fruitful, or value-returning functions, which So we cannot create our own variable or function with a name True — we'll get a in Python: one that returns a useful value, and the other that returns nothing, or None. But if the function should return a value, make sure all execution paths do� You can't. Variables defined inside a method are local to that method. If you want to share variables between methods, then you'll need to specify them as member variables of the class. Alternatively, you can pass them from one method to another as arguments (this isn't always applicable).

a) A method can hold multiple return statements, but only one return statement executes in one method call. 49) A programmer notices that the following code snippet uses the same algorithm for computing cost after taxes, but with different variables, in the two places as shown below, and in several other places in the program.

Comments
  • I'm interested, why do you want to pass around a method instead of declaring it in a scope that you can access on-demand?
  • Actually, the methods I need are static so they can be accessed. It's just I need to choose one to use depending on the value in choice, without doing the if-elseif check on choice every time through a loop.
  • You need to actually declare a delegate. This will allow you to essentially simulate function pointers in C# as you'd find them in C or C++.
  • Nice solution. You could also constrain BuildMethod to take only a value type as T, since a method that returns a reference type will be assignable to Func<object> already via a covariant method-group-to-delegate conversion.
  • @Eric: Except for method returning a nullable value type, which would neither satisfy a where T : struct constraint, nor work with generic covariance ;)
  • @Jon: Good point. I always forget that nullable types don't satisfy the value type constraint.
  • @Eric: I'll confess it wasn't the reason for not including a constraint to start with... but it sounds good after the fact :)
  • What about a method with parameters?
  • The methods don't return int, so this won't compile. Also, why do you declare your own version of Func<int>?