Click here to Skip to main content
Licence CC (ASA 2.5)
First Posted 6 Jul 2009
Views 27,163
Bookmarked 29 times

C# And Accepting Parameters

By | 9 Jul 2009 | Technical Blog
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.
A Technical Blog article. View original blog here.[^]

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 __argslist 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")
        ));

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

About the Author

webdev_hb



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalvery OO PinmemberDonsw18:50 3 Oct '09  
GeneralCAREFULL! There is a catch with the 'params' construct !! Pinmemberphilippe dykmans11:41 19 Jul '09  
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! Pinmemberwebdev_hb6:14 20 Jul '09  
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! Pinmemberphilippe dykmans6:44 20 Jul '09  
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! PinmemberS. Senthil Kumar8:39 24 Jul '09  
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! Pinmemberphilippe dykmans9:55 24 Jul '09  
GeneralRe: CAREFULL! There is a catch with the 'params' construct !! PinmemberCaneta7:50 21 Sep '09  
Generalusing (params object[] args) PinmemberMember 421602121:50 14 Jul '09  
GeneralRe: using (params object[] args) Pinmemberwebdev_hb1:20 15 Jul '09  
QuestionWhat about perfomance? PinmemberGLLNS23:43 13 Jul '09  
AnswerRe: What about perfomance? Pinmemberwebdev_hb1:17 14 Jul '09  
GeneralInteresting... PinmemberDragonfly_Lee23:09 9 Jul '09  
NewsUndocumented C# Types and Keywords PinmemberStiGMaTa_Dev21:13 9 Jul '09  
GeneralRe: Undocumented C# Types and Keywords PinmemberGLLNS23:38 13 Jul '09  
GeneralRe: Undocumented C# Types and Keywords Pinmemberwebdev_hb1:19 14 Jul '09  
GeneralRe: Undocumented C# Types and Keywords PinmemberGLLNS1:27 14 Jul '09  
GeneralRe: Undocumented C# Types and Keywords PinmemberBen Burnett12:24 14 Jul '09  
GeneralRe: Undocumented C# Types and Keywords Pinmemberwebdev_hb13:14 14 Jul '09  
GeneralRe: Undocumented C# Types and Keywords PinmemberBen Burnett13:41 14 Jul '09  
GeneralRe: Undocumented C# Types and Keywords Pinmemberwebdev_hb14:03 14 Jul '09  
Generalthanks.. Pinmembersxndave11:58 9 Jul '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 Jul 2009
Article Copyright 2009 by webdev_hb
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid