How does a method that can receive several parameters work?

12

According to the C # documentation:

  

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

However, I have a question regarding the parameters that are passed to it, this method seems to accept an unlimited amount of parameters, I can pass as many parameters as I want, see 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 that will be formatted and the other values that will be entered in string in>, then it was invoked with seven parameters.

My questions are:

  • How this method was set to have this accept behavior multiple parameters?
  • The amount of parameters I can pass to it is unlimited?
  • You can write methods like this that can accept a undefined number of parameters, if so, how?
  • And how can I call this practice?
  • asked by anonymous 20.03.2016 / 17:40

    2 answers

    7

    You can look at the source code for it to see. This mechanism is a parameter with variable amount 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++) {
                Console.Write(list[i] + " ");
            }
            Console.WriteLine();
        }
    }
    
    UseParams(1, 2, 3, 4);
    

    See working on dotNetFiddle .

    Retired from documentation .

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

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

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

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

    It seems like you never thought of having a way to limit the amount of arguments you can pass to this parameter. If you need it, the way it's really overloaded.

        
    20.03.2016 / 17:53
    7

    According to this answer in the SOen you should use the params modifier, like this:

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

    The call looks like this:

    AddUp(4, 5, 6);
    

    I'm not sure, but it used void in return , so it seems to me that it was a typo, although I have not tried it yet, but I believe that 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 params modifier after a certain parameter in a method, for example you want to force the first parameter to be a string and the others are int determined by you:

    public static string MeuMetodo(string primeiro, params int[] values)
    {
        Console.WriteLine(primeiro);
        Console.WriteLine(values);
    }
    
        
    20.03.2016 / 17:49