Does C# support variable arguments (vararg's) on methods?

Published 05 August 04 05:00 PM

There was a suggestion/question on the Product Feedback site for VS2005 that suggested that the params keyword may not be that well understood.

The params keyword can be applied on a method parameter that is an array.  When the method is invoked, the elements of the array can be supplied as a comma separated list.

So, if the method parameter is an object array,

void paramsExample(object arg1, object arg2, params object[] argsRest)

{

foreach (object arg in argsRest)

{ /* .... */ }

}

 

then the method can be invoked with any number of arguments of any type.

paramsExample(1, 0.0f, "a string", 0.0m, new UserDefinedType());

 

[SantoshZ]

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Eric Newton said on August 6, 2004 6:40 PM:
Oh great, here come the people complaining about a completely open ended interface... I can just hear the thunder now.

TIDAL WAVE!
# AndrewSeven said on August 9, 2004 1:53 PM:
That example seems like a bad idea.

I try to only use params to pass a variable number of the same things. In fact it is mostly strings.
To pass an array of object where the objects will be of arbitrary types is going to a dangerous place.
# Flier Lu said on August 9, 2004 10:51 PM:
I wonder why vararg mode is so slow than pass by array ?

public class EntryPoint
{
[DllImport("msvcrt.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
extern static int printf(string format, __arglist);

public static void Main(String[] args)
{
printf("%s %d", __arglist("Flier Lu", 1024));
}
}

btw: Here is some articls from my blog about vararg mode.

http://www.blogcn.com/User8/flier_lu/index.html?id=2602611
http://www.blogcn.com/User8/flier_lu/index.html?id=2602647
# RebelGeekz said on December 28, 2004 4:53 AM:
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker