Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

C# And Accepting Parameters

Rate me:
Please Sign up or sign in to vote.
4.29/5 (15 votes)
9 Jul 2009CC (ASA 2.5)1 min read 61.5K   30   21
C# offers a few additional keywords to allow you to accept multiple parameters for a method. This post goes over using the 'params' keyword and the evil '__arglist' keyword.

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

C#
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.

C#
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 __argslist keyword?

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

C#
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")
        ));

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.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalvery OO Pin
Donsw3-Oct-09 18:50
Donsw3-Oct-09 18:50 
GeneralCAREFULL! There is a catch with the 'params' construct !! Pin
philippe dykmans19-Jul-09 11:41
philippe dykmans19-Jul-09 11:41 
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! Pin
webdev_hb20-Jul-09 6:14
webdev_hb20-Jul-09 6:14 
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! Pin
philippe dykmans20-Jul-09 6:44
philippe dykmans20-Jul-09 6:44 
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! Pin
S. Senthil Kumar24-Jul-09 8:39
S. Senthil Kumar24-Jul-09 8:39 
philippe dykmans wrote:
A C# bug?


Not really. The method signatures match exactly in the second case (where you pass an object[]), and the params modifier doesn't come into the picture at all.

Regards
Senthil
_____________________________
My Home Page |My Blog | My Articles | My Flickr | WinMacro

GeneralRe: CAREFULL! There is a catch with the 'params' construct !! Pin
philippe dykmans24-Jul-09 9:55
philippe dykmans24-Jul-09 9:55 
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! Pin
Caneta21-Sep-09 7:50
Caneta21-Sep-09 7:50 
Generalusing (params object[] args) Pin
PaulMoor14-Jul-09 21:50
PaulMoor14-Jul-09 21:50 
GeneralRe: using (params object[] args) Pin
webdev_hb15-Jul-09 1:20
webdev_hb15-Jul-09 1:20 
QuestionWhat about perfomance? Pin
GLLNS13-Jul-09 23:43
GLLNS13-Jul-09 23:43 
AnswerRe: What about perfomance? Pin
webdev_hb14-Jul-09 1:17
webdev_hb14-Jul-09 1:17 
GeneralInteresting... Pin
Dragonfly_Lee9-Jul-09 23:09
Dragonfly_Lee9-Jul-09 23:09 
NewsUndocumented C# Types and Keywords Pin
StiGMaTa_Dev9-Jul-09 21:13
StiGMaTa_Dev9-Jul-09 21:13 
GeneralRe: Undocumented C# Types and Keywords Pin
GLLNS13-Jul-09 23:38
GLLNS13-Jul-09 23:38 
GeneralRe: Undocumented C# Types and Keywords Pin
webdev_hb14-Jul-09 1:19
webdev_hb14-Jul-09 1:19 
GeneralRe: Undocumented C# Types and Keywords Pin
GLLNS14-Jul-09 1:27
GLLNS14-Jul-09 1:27 
GeneralRe: Undocumented C# Types and Keywords Pin
Ben Burnett14-Jul-09 12:24
Ben Burnett14-Jul-09 12:24 
GeneralRe: Undocumented C# Types and Keywords Pin
webdev_hb14-Jul-09 13:14
webdev_hb14-Jul-09 13:14 
GeneralRe: Undocumented C# Types and Keywords Pin
Ben Burnett14-Jul-09 13:41
Ben Burnett14-Jul-09 13:41 
GeneralRe: Undocumented C# Types and Keywords Pin
webdev_hb14-Jul-09 14:03
webdev_hb14-Jul-09 14:03 
Generalthanks.. Pin
sxndave9-Jul-09 11:58
sxndave9-Jul-09 11:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.