Functions with variable parameters in C#

While developing in C# (or any programming language for that matter) you may invariably find your self in need of using overridden functions for accepting different no of parameters.

Let me illustrate with a small example:

Consider a case where you want to add some numbers, if you know how many numbers you are going to add before hand, this may not concern you. But what if your application demands that the numbers can vary, what are you going to do in this case? Are you going to declare one function for all the possible cases? How many cases are you going to handle? Are you going to use the following strategy?

Add 2 numbers
public void Add(int1, int2){…}

Add 3 numbers
public void Add(int1, int2, int3){…}

and so on….?

Would not it be nice if you could supply variable no of parameters? Something like

public void Add(int1,int2, int3…….. )

Well as a matter a fact C# does allow you to do something like that. C# provides the following keyword for parameters for in a function

  • ref
  • out
  • params

What we are interested over here is the “params” keyword (we wont delve into other keywords :)). The “params” lets you specify a parameter which has variable length. So you could write a function which adds arbitrary numbers in the following manner.

public void add(params int[] no)
    {
    int count = no.Length;
    int sum = 0;
    for(int i =0; i < count; i++)
    {
        sum += no[i];
    }

Now you can call this method from other functions. To call a function you just call it as follows:

add(1,2,3,2,4,67,34);

add(1,2,3,2);

No need for overriding the function for all the possible variations.

NOTE: i have used int just for illustration purpose  over here.

If you want to use other types (different types of objects), you can specify Object array instead of Int array, since all objects are internally inherited from system.object class, any object can be passed to the function.

For e.g

public void SomeMethod(params Object[] obs){…}

Now you can pass any object to “SomeMethod”, so you could very well call it like this

SomeMethod(1,”somestring”, new date());

or

SomeMethod(new CustomClass(), new CustomClass2()… )

Note: You will need to handle the parameters passed to the function explicitly through code for type safety.

The following conditions apply for the “params” keyword in the function

  • The parameter with “params” keyword has to be the last paramter, i.e. you cannot define more parameters after the parameter which is marked as variable.
  • There can be only one parameter marked as variable.

Its a neat little utility keyword, may come in handy sometime or other. :) 

Refer MSDN for more information.

About these ads

2 Comments to “Functions with variable parameters in C#”

  1. Long time you wrote this but now it helped me.

    Exactly what I need – thanks!

  2. If some one wants expert view concerning running a blog after that i advise him/her to visit
    this blog, Keep up the good work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: