0

Is possible to create method with dynamic number of parameter in C#?

For example

Public void sum(dynamic arguments//like JavaScript)
{
   //loop at run-time  on arguments and sum
}

Can I use Dynamic Object?

4
  • 1
    are you looking for params? bla(params int[] variableInts) Commented Mar 3, 2013 at 18:35
  • 1
    How about this: google.com/… Commented Mar 3, 2013 at 18:37
  • 1
    It would really have helped if you'd posted an example of how you want to call the method. Commented Mar 3, 2013 at 18:42
  • Are you looking for variable number of arguments or the dynamic type as parameter? and could you update your question accordingly, kinda vague atm. Commented Mar 3, 2013 at 18:45

3 Answers 3

3

Use the params keyword to achieve a variable number of arguments.

The params keyword lets you specify a method parameter that takes a variable number of arguments. You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type. You also can send no arguments.

For example: public void Sum( params int[] args ){ }

Can I use Dynamic Object?

Yes, but possibly not in the way you are thinking.

// example 1 - single parameter of type dynamic
private static void Sum( dynamic args ) { }

// will not compile; Sum() expects a single parameter whose type is not
// known until runtime
Sum( 1, 2, 3, 4, 5 );

// example 2 - variable number of dynamically typed arguments
private static void Sum( params dynamic[] args ) { }

// will compile
Sum( 1, 2, 3, 4, 5 );

So you could have a method such as:

public static dynamic Sum( params dynamic[] args ) {

    dynamic sum = 0;

    foreach( var arg in args ){
        sum += arg;
    }

    return sum;
}

And call it: Sum( 1, 2, 3, 4.5, 5 ). The DLR is smart enough to infer the correct type from the arguments, and the returned value will be System.Double. However (at least in the case of a Sum() method), giving up explicit control over type specification and losing type safety seems like a bad idea.

I'm assuming you have a reason for not using Enumerable.Sum()

Sign up to request clarification or add additional context in comments.

2 Comments

dynamic is used when you want to wait until runtime to resolve the type; A sum method implies that you do know the type.
private static void Sum( params dynamic[] args ) will compile, if that's what you are asking.
1

yes, you can take a look here

http://msdn.microsoft.com/library/w5zay9db(v=vs.80).aspx

(the params keyword)

Comments

1

Maybe an example unit test clarifies things slightly:

    [Test]
    public void SumDynamics()
    {
        // note that we can specify as many ints as we like
        Assert.AreEqual(8, Sum(3, 5)); // test passes
        Assert.AreEqual(4, Sum(1, 1 , 1, 1)); // test passes
        Assert.AreEqual(3, Sum(3)); // test passes
    }

    // Meaningless example but it's purpose is only to show that you can use dynamic 
    // as a parameter, and that you also can combine it with the params type.
    public static dynamic Sum(params dynamic[] ints)
    {
        return ints.Sum(i => i);
    }

Be aware when using dynamics, you are telling your compiler to back off, so you will get your exceptions at run-time.

    [Test, ExpectedException(typeof(RuntimeBinderException))]
    public void AssignDynamicIntAndStoreAsString()
    {
        dynamic i = 5;
        string astring = i; // this will compile, but will throw a runtime exception
    }

Read more about dynamics.

Read more about params

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.