C# params object[] strange behavior

前端 未结 3 1015
渐次进展
渐次进展 2020-12-01 15:59

Considering this code

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strings          


        
3条回答
  •  情话喂你
    2020-12-01 16:59

    Im not an expert but the params keyword idea was to enable the posibility to make different calls to the method independent of how many elements you have.

    Test(object1)
    Test(object1, object2)
    Test(object1,..., objectN)
    

    So what you are seeing is the normal behaviour nothing weird. More info on this link msdn

    By using the params keyword, you can specify a method parameter that takes a variable number of arguments.

    You can send a comma-separated list of arguments of the type specified in the parameter declaration or an array of arguments of the specified type. You also can send no arguments. If you send no arguments, the length of the params list is zero.

    No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

提交回复
热议问题