Posts

Showing posts with the label variadic

Why use the params keyword?

Why use the params keyword? I know this is a basic question, but I couldn't find an answer. Why use it? if you write a function or a method that's using it, when you remove it the code will still work perfectly, 100% as without it. E.g: With params: static public int addTwoEach(params int args) { int sum = 0; foreach (var item in args) sum += item + 2; return sum; } Without params: static public int addTwoEach(int args) { int sum = 0; foreach (var item in args) sum += item + 2; return sum; } The code of the method itself will still work perfectly... the calling code may well not... – Jon Skeet Sep 28 '11 at 8:30 params key word means OPTIONAL parameters that can be passed or not to the Method. An array with out params key word means you MUST pass array argument ...