Sunday, June 19, 2011

Using __arglist to send parameters to a Method

__arglist is used to send parameters to a method. Generally we used to send parameters to a function by having a list of arguments specified in the method head. If we want to pass a new set of arguments we need to have Method Overloading. Even when we want to send any number of arguments we might use param array.

Now why should we use __arglist.
In case of each of these methods, the problems are :

1. If we use Method Overloading, we might have to add new methods whenever a new set of argument list is thought to be sent.

2. If we use param array we need to have same type of arguments or need to have param array of objects.
__arglist reveals each of those. It can send any no of argument to a function. It might be of any type and we can parse each argument easily using simple steps.
Lets have a look at the Code :
public int paramLength(__arglist)
 {
    ArgIterator iterator = new ArgIterator(__arglist);
   return iterator.GetRemainingCount();
}

Now if I call the function using this statement

int x = this.paramLength(__arglist(49,34,54,6,"asdfg")); // returns 5

Read more: Daily .Net Tips