Monday, June 4, 2007

Variable arguments in C#

Ever used variable arguments in C++ and wanted to use them in C# but you could never figure out how? I certainly did a few weeks ago, I was working on a function similar to some of the old standard C IO libraries (though I can't recall what it's purpose was to be) and I spent quite a bit of time trying to learn how, never actually finding anything. Well I just stumbled upon it here at the MSDN C# FAQ page.

If you're like me and would rather just see the code...

void paramsExample(object arg1, object arg2, params object[] argsRest)
    foreach (object arg in argsRest)
    { /* .... */ }


Christopher Anderson said...
This comment has been removed by the author.
Christopher Anderson said...

You rock. I don't know if I'm a lazy coder, but I love how you put the code up with "all I want to see is the code."

CAWYM. (Couldn't agree with you more). Okay, I'm lazy.

If the code looks good, then I will read the text :)

We're coders, not authors. We read code!