user1968030
user1968030

Reputation:

Create Method that has dynamic number of parametr

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?

Upvotes: 0

Views: 155

Answers (3)

bas
bas

Reputation: 14902

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

Upvotes: 1

Tim M.
Tim M.

Reputation: 54359

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()

Upvotes: 3

user287107
user287107

Reputation: 9418

yes, you can take a look here

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

(the params keyword)

Upvotes: 1

Related Questions