.Net Dojo: Parameter Lists
I once attended a Microsoft Technet Event where they were showing off the new (at the time) .Net 3.0 features. As a self-taught .Net developer, I’m always looking for little tricks and shortcuts and cool things that make my life easier. As I watched the Demo, one thing I noticed was they were using a method that allowed for the use of an infinate number of parameters of the same type. I went home after the conference and immediately began researching what this infinate parameter was and how to use it in my own methods.
I quickly discovered that a commonly used construct, String.Format, also uses this parameter list. By looking at the method I was easily able to deduce how the parameter list worked, and have been taking advantage of it’s usefullness ever since. A parameter list is simply an array of indefinite size of a specified type. In the case of String.Format, the parameter list is an object array. The only limitation is that each method is only allowed 1 parameter list, and it must be the last parameter declared. Let’s take a look at how to make our own methods with a parameter list.
public static void DoSomething(params object[] arguments)
{
foreach (object arg in arguments)
{
//...Do something here
}
}
As you can see, implementation is very easy. Add the keyword params in front of the array type, and inside the method you can iterate the array. It’s a very handy tool to use when passing in a list. If you’ve ever used an arraylist as a parameter, this is a much better way, unless you need multiple lists in the same method. Object is merely an example, but you can use anything, even your own classes. To call the above method, we could create an object array and pass it in, but why do things the hard way?
DoSomething(new object(), new object(), new object());
If we must create the list before passing it in, use the power of generics.
List<object> myParams = new List<object>(); myParams.Add(new object()); myParams.Add(new object()); myParams.Add(new object()); DoSomething(myParams.ToArray());
ToArray() will turn the list onto an array of the List’s type. Using parameter lists offers a great amount of flexibility, especially in situations where you have 1 library used by multiple developers. It allows you to pass in any number of same-type variables. Note that it would be bad design to use this for an array where the order of the strings matters, i.e, if you have a method that takes in a user’s first name, last name, email address, etc, as a string array. It’s intended use is for a collection of objects where order does not have any special meaning.
ADDITION: (April 3, 2008)
You can use the params keyword on a list of items where the order or naming matters by making the parameter list of the type System.Collections.KeyValuePair. This makes your array accept a “dictionary” of objects that can be declared in the method call intead of pre-building a Dictionary object and passing it in. Sometimes I will even use an enum class I have created as the type for the key so that knowing the string values of the key names is unnecessary.
January 28, 2008 - Posted by Broken Bokken | .Net | .Net, .Net 2.0, .Net Dojo, ASP.NET, C#, Development, Generics, List, Parameter, Parameter Lists, Params, Programming, Technology | 2 Comments
2 Comments »
Leave a comment
Categories
- Anime (3)
- Japanese (9)
- Life (4)
- Martial Arts (4)
- Personal (14)
- Recipes (2)
- Shinkendo (2)
- Storm Chasing (1)
- Tech-Knowledge (14)
- .Net (13)
Archived Articles
Favorites
Friends
Resources
-
Archives
- June 2008 (3)
- May 2008 (1)
- March 2008 (1)
- February 2008 (6)
- January 2008 (6)
- November 2007 (18)
-
Categories
-
RSS
Entries RSS
Comments RSS
Thanks for the tip, it’s very useful!
I’m glad you found it useful! Thanks for reading.