Hugoware

The product of a web developer with a little too much caffeine

C# And Accepting Parameters

with 2 comments

Have you ever written a function that looked similar to the following – Passing in an array of a value?

public void UpdateList(string[] list) {
    //...etc...
}

It’s not that uncommon of a method, but did you know you can take it a little further?

The params Keyword

The params keyword is an interesting modifier to an argument for a method. It allows a user to pass individual values instead of using the array itself.

public void ProcessList(params string[] args) {
    //...etc...
}

//old way still works
ProcessList(new string[] { "a", "b", "c" });

//new way works as well
ProcessList("a", "b", "c");

You’ll notice you can still pass in an array like before, but now you can add each parameter one at a time.

The params keyword has to be the last parameter in your method, but other than that, there isn’t much more to it. You treat the argument just like you would any other array.

More Arguments Using ‘Pure Evil’

The params keyword gives you a little more flexibility when working with arguments, but have you ever heard about the __arglist keyword?

If not, it’s not that big of a deal — You really shouldn’t use it that much.

public static void CallArgList(__arglist) {
ArgIterator args = new ArgIterator(__arglist);
int total = args.GetRemainingCount();
for (int i = 0; i < total; i++) { object value = TypedReference.ToObject(args.GetNextArg()); Console.WriteLine( "Arg #{0}: {1} ({2})", i, value, value.GetType() ); } } //then used like... CallArgList( __arglist( "Jim", 1, false, (new StringBuilder()).Append("howdy") )); [/sourcecode] Now, that is some strange looking code but even stranger, it works! We're now able to pass in values into our method without any constraints. I don't really recommend using this keyword. Writing a similar method using an object array (using params) found no real difference in the speed of execution. Given that it isn't a commonly used feature there is a fair chance that other developers that stumble across it will have to hit Google before they can go any further.

Advertisements

Written by hugoware

July 6, 2009 at 6:44 am

2 Responses

Subscribe to comments with RSS.

  1. C# And Accepting Parameters « Yet Another WebDev Blog…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

    DotNetShoutout

    July 6, 2009 at 6:48 am

  2. The ‘__arglist’ is a good way to make Visual Studio go BOOM as well… Don’t try this at home, kids…

    var m = __arglist();
    m. <– **KABOOOM**

    webdev_hb

    July 7, 2009 at 10:39 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: