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 

5 will be returned to the variable x. It is because we send 5 arguments to the method. We can access each methods using :

 TypedReference tf = iterator.GetNextArg(); 
TypedReference.ToObject(tf)
 

On each call to GetNextArg, the GetRemainingCount will decrease by one until it wipes out each objects set to the iterator.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434