Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A delegate for a function with variable parameters

Tags:

c#

delegates

func

I have a function of this sort

void func(params object[] parameters) { 
    //Function Body
}

It can accept parameters of the following sort

func(10, "hello", 30.0);
func(10,20);

and so on.

I wanted to create an Action delegate for the above function. Is it possible? If not then why?

like image 332
SohamC Avatar asked Sep 05 '14 16:09

SohamC


People also ask

How do you pass parameters to delegates?

You can pass methods as parameters to a delegate to allow the delegate to point to the method. Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class.

What is a delegate method?

A delegate method is a method that the delegate object is expected to implement. Some delegate methods are required, while some are not. In IOS, most delegates are expected to conform to an Objective-C protocol; the protocol declaration will tell you which methods are optional and which are required.

How do you call a function using delegates?

Delegates can be invoke like a normal function or Invoke() method. Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method.

What is function delegate in C#?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.


Video Answer


1 Answers

You can't use the existing Action delegates with params, but you can declare your own delegate that way:

public delegate void ParamsAction(params object[] arguments)

Then:

// Note that this doesn't have to have params, but it can do
public void Foo(object[] args)
{
    // Whatever
}

...

ParamsAction action = Foo;
action("a", 10, 20, "b");

Of course you can create an Action<object[]> for your existing method - but you lose the params aspect of it, as that's not declared in Action<T>. So for example:

public static void Foo(params object[] x)
{
}

...

Action<object[]> func = Foo;
func("a", 10, 20, "b"); // Invalid
func(new object[] { "a", 10, 20, "b" }); // Valid

So if you're calling the delegate from code which wants to use params, you need a delegate type which includes that in the declaration (as per the first part). If you just want to create a delegate which accepts an object[], then you can create an instance of Action<object[]> using a method which has params in its signature - it's just a modifier, effectively.

like image 63
Jon Skeet Avatar answered Sep 22 '22 09:09

Jon Skeet