How does a method that can receive multiple parameters work?

Asked

Viewed 1,007 times

14

According to the C documentation#:

The method String.Format is responsible for converting the value of objects in strings based on the specified formats and inserts them in another character string.

However, I have a doubt regarding the parameters that are passed to him, this method seems to accept an unlimited amount of parameters, I can pass as many parameters as I want, see the example:

class Program
{
    static void Main(string[] args)
    {
        string numeros = string.Format("Numeros: {0}, {1}, {2}.", "Um", "Dois", "Tres");
        string nomes = string.Format("Nomes: {0}, {1}, {2}, {3}, {4}, {5}.", "Joao", "Maria", "Junior", "Carvalho", "Leticia", "Silva");

        Console.WriteLine(nomes);
        Console.WriteLine(numeros);
    }
}

In the example the method was invoked with four parameters, the first being the string which will be formatted and the other values that will be inserted in the string, then he was invoked with seven parameters.

My doubts are:

  1. How this method was defined to have this accepting behavior various parameters?
  2. The amount of parameters I can pass to it is unlimited?
  3. It is possible to write methods like this that can accept a indefinite amount of parameters, if yes, how to do?
  4. And what can I call this practice?

2 answers

9


You can look at the his source code to see. This mechanism is a parameter with variable number of arguments.

The quantity is virtually unlimited and can be used in any method you want, as long as it is the last parameter:

public class MyClass {
    public static void UseParams(params int[] list) {
        for (int i = 0; i < list.Length; i++) Write(list[i] + " ");
        WriteLine();
    }
}

UseParams(1, 2, 3, 4);

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Taken from documentation.

The keyword params is what determines that this parameter will work like this.

You can see that in the background is a single parameter of the type array of something (some kind). It is often used a Object[] to accept anything at all. So although the method call syntax passes several arguments, they are encapsulated all in this array.

There is a proposal for C# 8 or further to accept that the parameter is an enumerable and not just a array.

There is a mechanism of optional arguments which can be confused with this mechanism in some situations. Remembering that the amount of parameters is always defined in both cases. Both avoid doing too much overloding methods, which in the case of varargs would be almost impractical.

It seems that they have never thought of having a way to limit the amount of arguments that can be passed to this parameter. If you need it, the way to do it is to really overload.

  • It seems that it is also possible to use ... or I’m wrong?

  • @Wallacemaxters is not possible in C#, which is a shame, but it doesn’t make that much difference.

8

As stated in Soen you must use the modifier params, in this way:

public static int AddUp(params int[] values)
{
    int sum = 0;
    foreach (int value in values)
    {
        sum += value;
    }
    return sum;
}

The call went like this:

AddUp(4, 5, 6);

A method with strings and return would look like this:

public static string MeuMetodo(params string[] values)
{
    return String.Join(" ", values);
}

And to use it would look like this:

MeuMetodo("Olá", "Mundo", "!");

You can also use the modifier params after a certain parameter in a method, for example you want to force the first parameter to be a string and the others to be int determined by you:

public static string MeuMetodo(string primeiro, params int[] values)
{
    Console.WriteLine(primeiro);
    Console.WriteLine(values);
    return String.Join(" ", values);
}

IDEONE: https://ideone.com/kgTLx1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.