.NET Tip: Passing a Variable Number of Arguments to a Method Wisconsin

Have you ever not known how many parameters a method would need to accept? The C# params keyword lets you pass a variable number of arguments to a method.

Local Companies

Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
Electroniclaim
262-240-9700
11357 N. Port Washington Rd
Mequon, WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
HarrisData
800-225-0585
13555 Bishops Court
Brookfield, WI


.NET Tip: Passing a Variable Number of Arguments to a Method

provided by: 
Originally published at Internet.com


If you need to pass an unknown number of arguments to a method, the params keyword is exactly what you need. With params, you can pass a variable number of parameters to a method and any parameters after the fixed parameters will be collected into an array and passed to your method. You can only use the params keyword for one parameter in your method declaration and it must always be the last parameter. A method that accepts a string and then a variable number of parameters of any type looks like this: public void ObjectParams(string Message, params object[] p) { Console.WriteLine(Message); for (int i = 0; i < p.Length; i++) { Console.WriteLine(p[i]); } }

This method prints the string parameter to the console then loops through variable parameters, printing each one to the console as well. It accepts an array of objects, so the parameters can be any data type. You would call the function like this: ObjectParams("Variable Object Parameters", 12, 'z', "Test");

If you know the data type of your arguments, you can optimize this somewhat by specifying a type other than object for the variable parameters. A method that expects a variable number of integers would look like this: public void IntParams(string Message, params int[] p) { Console.WriteLine(Message); for (int i = 0; i < p.Length; i++) { Console.WriteLine(p[i]); } }

You call this method the same way as before, only passing integers. IntParams("Variable Integer Parameters", 1, 2, 3, 4, 5);

You also can pass an array as the last parameter instead of passing individual parameters. The previous method, which accepts integers, also could be called this way. int[] TestIntArray = new int[5] { 11, 12, 13, 14, 15 }; IntParams("Integer Array Parameter", TestIntArray);

The ability for a method to accept a variable number of parameters can come in handy in many situations. Now, with the params keyword, you have what you need to take advantage of this ability in C#.

About the Author

Jay Miller is a Software Engineer with Electronic Tracking Systems, a company dedicated to robbery prevention, apprehension, and recovery based in Carrollton, Texas. Jay has been working with .NET since the release of the first beta and is co-author of Learn Microsoft Visual Basic.Net In a Weekend. Jay can be reached via email at jmiller@sm-ets.com.

Author: Jay Miller

Read article at Internet.com site

Featured Local Company

HarrisData

800-225-0585
13555 Bishops Court
Brookfield, WI
http://www.harrisdata.com

Regional Articles
- .NET Tip: Passing a Variable Number of Arguments to a Method Appleton WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Baraboo WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Beaver Dam WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Beloit WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Brookfield WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Burlington WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Cedarburg WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Chippewa Falls WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Cudahy WI
- .NET Tip: Passing a Variable Number of Arguments to a Method De Pere WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Delavan WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Eau Claire WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Elkhorn WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Fond Du Lac WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Fort Atkinson WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Franklin WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Green Bay WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Hartland WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Janesville WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Kaukauna WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Kenosha WI
- .NET Tip: Passing a Variable Number of Arguments to a Method La Crosse WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Lake Geneva WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Manitowoc WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Marinette WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Marshfield WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Menasha WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Menomonee Falls WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Menomonie WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Merrill WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Middleton WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Milwaukee WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Mosinee WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Mukwonago WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Muskego WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Neenah WI
- .NET Tip: Passing a Variable Number of Arguments to a Method New Berlin WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Oak Creek WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Oconomowoc WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Onalaska WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Oshkosh WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Pewaukee WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Racine WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Rhinelander WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Rice Lake WI
- .NET Tip: Passing a Variable Number of Arguments to a Method River Falls WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Schofield WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Shawano WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Sheboygan WI
- .NET Tip: Passing a Variable Number of Arguments to a Method South Milwaukee WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Stevens Point WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Sturgeon Bay WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Sun Prairie WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Superior WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Thiensville WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Two Rivers WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Watertown WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Waukesha WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Waupaca WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Wausau WI
- .NET Tip: Passing a Variable Number of Arguments to a Method West Bend WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Whitewater WI
- .NET Tip: Passing a Variable Number of Arguments to a Method Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Home Services Software
Business Services Entertainment Industrial Goods & Services Technology
Career Family Insurance Telecommunications
Cars Financial Services Internet Transportation & Logistics
Computer Hardware Food & Beverage Legal Travel
Construction Health Real Estate Wedding
Education Home Electronics Retail & Consumer Services