A delegate for a function with variable parameters

SohamC

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?

Jon Skeet

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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Best practice for unused function parameters

来自分类Dev

Statsmodels OLS function for multiple regression parameters

来自分类Dev

Bash: Update the parent variable from function variable

来自分类Dev

qsort: Cast the comparator function itself or the parameters in the body of comparator function?

来自分类Dev

Cannot update a variable inside a function

来自分类Dev

Trying to pass variable to Wordpress function

来自分类Dev

What is the contract of a function or variable in TypeScript

来自分类Dev

Is it possible to create a throttle function that can take in as parameters another function (that also has parameters), and the time delay

来自分类Dev

如何在 cmd.Parameters.AddWithValue("@" & Variable & "" , "") 中指定变量

来自分类Dev

Can parameters from a parametrized class be used in external function definitions?

来自分类Dev

How to pass unlimited arguments and one or two parameters to a JavaScript function?

来自分类Dev

Why this code passing switch parameters to a PowerShell function fails

来自分类Dev

Friend function that takes 2 classes in parameters - 'Class' not defined

来自分类Dev

subs 或 function-parameters 中的问号是什么意思?

来自分类Dev

Working with a global variable inside a python function

来自分类Dev

Retrieve value by variable name in erlang function

来自分类Dev

C++ static variable in member function

来自分类Dev

“ variable:function(){}”是什么意思?

来自分类Dev

Passing external function of multiple variables as a function of one variable in Fortran

来自分类Dev

UITextField delegate methods not called

来自分类Dev

Use "this" referring anonymous delegate

来自分类Dev

RACDelegateProxy与Common Delegate的实现

来自分类Dev

How do I redirect the output of a system() function to a variable?

来自分类Dev

AngularJS and HTML: Escaping quotes inside a variable for an onclick function

来自分类Dev

How do I turn a function variable into a div id name?

来自分类Dev

Clojure: issues passing a bound variable by doseq to another function

来自分类Dev

Export function that names the export file after the input variable

来自分类Dev

Why does printing a variable and function call together in this code give an error?

来自分类Dev

How do you use #call directive for Red function with string datatypes as parameters?

Related 相关文章

  1. 1

    Best practice for unused function parameters

  2. 2

    Statsmodels OLS function for multiple regression parameters

  3. 3

    Bash: Update the parent variable from function variable

  4. 4

    qsort: Cast the comparator function itself or the parameters in the body of comparator function?

  5. 5

    Cannot update a variable inside a function

  6. 6

    Trying to pass variable to Wordpress function

  7. 7

    What is the contract of a function or variable in TypeScript

  8. 8

    Is it possible to create a throttle function that can take in as parameters another function (that also has parameters), and the time delay

  9. 9

    如何在 cmd.Parameters.AddWithValue("@" & Variable & "" , "") 中指定变量

  10. 10

    Can parameters from a parametrized class be used in external function definitions?

  11. 11

    How to pass unlimited arguments and one or two parameters to a JavaScript function?

  12. 12

    Why this code passing switch parameters to a PowerShell function fails

  13. 13

    Friend function that takes 2 classes in parameters - 'Class' not defined

  14. 14

    subs 或 function-parameters 中的问号是什么意思?

  15. 15

    Working with a global variable inside a python function

  16. 16

    Retrieve value by variable name in erlang function

  17. 17

    C++ static variable in member function

  18. 18

    “ variable:function(){}”是什么意思?

  19. 19

    Passing external function of multiple variables as a function of one variable in Fortran

  20. 20

    UITextField delegate methods not called

  21. 21

    Use "this" referring anonymous delegate

  22. 22

    RACDelegateProxy与Common Delegate的实现

  23. 23

    How do I redirect the output of a system() function to a variable?

  24. 24

    AngularJS and HTML: Escaping quotes inside a variable for an onclick function

  25. 25

    How do I turn a function variable into a div id name?

  26. 26

    Clojure: issues passing a bound variable by doseq to another function

  27. 27

    Export function that names the export file after the input variable

  28. 28

    Why does printing a variable and function call together in this code give an error?

  29. 29

    How do you use #call directive for Red function with string datatypes as parameters?

热门标签

归档