Creating methods with infinite parameters?

后端 未结 8 1618
再見小時候
再見小時候 2021-01-31 13:44

In C# you can do this:

foo = string.Format(\"{0} {1} {2} {3} ...\", \"aa\", \"bb\", \"cc\" ...);

This method Format() accepts in

相关标签:
8条回答
  • 2021-01-31 14:36
        public static void TestStrings(params string[] stringsList)
        {
            foreach (string s in stringsList){ } 
                // your logic here
        }
    
    0 讨论(0)
  • 2021-01-31 14:36
    function void MyFunction(string format, params object[] parameters) {
    
    }
    

    Instad of object[] you can use any type your like. The params argument always has to be the last in the line.

    0 讨论(0)
  • 2021-01-31 14:37

    A few notes.

    Params needs to be marked on an array type, like string[] or object[].

    The parameter marked w/ params has to be the last argument of your method. Foo(string input1, object[] items) for example.

    0 讨论(0)
  • 2021-01-31 14:41

    use the params keyword. For example

    static void Main(params string[] args)
    {
        foreach (string arg in args)
        {
            Console.WriteLine(arg);
        }
    }
    
    0 讨论(0)
  • 2021-01-31 14:41

    You can achieve this by using the params keyword.

    Little example:

    public void AddItems(params string[] items)
    {
         foreach (string item in items)
         { 
             // Do Your Magic
         }
    }
    
    0 讨论(0)
  • 2021-01-31 14:42

    Use the params keyword. Usage:

    public void DoSomething(int someValue, params string[] values)
    {
        foreach (string value in values)
            Console.WriteLine(value);
    }
    

    The parameter that uses the params keyword always comes at the end.

    0 讨论(0)
提交回复
热议问题