D
D
Denis Bredun2021-06-07 23:48:25
C++ / C#
Denis Bredun, 2021-06-07 23:48:25

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

1 answer(s)
V
Vasily Bannikov, 2021-06-07
@Luffy1

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});

I can’t give another example from practice, because I don’t remember, but there are such.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question