Answer the question
In order to leave comments, you need to log in
When is it appropriate to use params?
Please describe general situations when it is appropriate to use params, and also situations when it is better to stuff an indefinite number of arguments with params into a method, instead of a one-dimensional array. Also, please provide an example/several examples of the use of appropriate params.
Answer the question
In order to leave comments, you need to log in
params - This is sugar for passing a one-dimensional array.
It is advisable to use it where these values are specified at the stage of writing the code, instead of passing an arbitrary array.
For example, in string.Format and other similar methods (ILogger, parameters for SQL queries, parameters for HTTP requests)
Both examples are valid and basically do exactly the same thing, but with params it looks and reads nicer/easier:
string.Format("{0}, {1}", 1, 2);
string.Format("{0}, {1}", new object[] {1,2});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question