params keyword  
Author Message
luiz s.





PostPosted: Visual C# General, params keyword Top

Hi, what's the difference between

private void UseParams(int[] i);

and

private void UseParams2(params int[] i);




Visual C#3  
 
 
Brendan Grant





PostPosted: Visual C# General, params keyword Top

To call the first you’d need to explicitly declare an integer array and pass it in while in the second the array is generated for you behind the scenes.

Params is very useful in cases when you want to allow the caller of a method the ability to pass in an arbitrary count of data without having to require them to create an array.

One area you find this often is with methods like Console.Writeline() where you can specify your format string (ie the thing with {0}, {1}, etc) and then pass in the values that will fill in those gaps.

I should mention that while using params *can* generate the array behind the scenes for you... it doesn’t have to, you yourself can manually create an array of the appropriate type and pass it in yourself.

Does this clear things up



 
 
luiz s.





PostPosted: Visual C# General, params keyword Top

Yes, definitely!

thanks!