C# params variable parameters

1. Introduction

The params keyword is used to define a variable parameter list for a method. It allows methods to accept any number of parameters without specifying the number of parameters in advance. Using the params keyword, methods can be invoked more flexibly without having to write multiple method overloads each for a different number of parameters. To use the params keyword, variable parameters need to be marked as params keyword in the method's parameter list. This parameter must be an array type and must be the last parameter in the method's parameter list. When calling a method, any number of parameters can be passed, which will be packaged as an array and passed to the method as the params parameter.

Two, usage

the case

namespace ParamsTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            PrintNumbers(1, 2, 3, 4, 5);

            Console.ReadKey();
        }

        static void PrintNumbers(params int[] numbers)
        {
            foreach (int number in numbers)
            {
                Console.WriteLine(number);
            }
        }
    }
}

run:

It is also possible to add other parameters in front of the variable parameters

namespace ParamsTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            PrintNumbers("fadf", 1, 2, 3, 4, 5);

            Console.ReadKey();
        }

        static void PrintNumbers(string name, params int[] numbers)
        {
            Console.WriteLine(name);
            foreach (int number in numbers)
            {
                Console.WriteLine(number);
            }
        }
    }
}

run:

Note 1

The variable parameter must be the last parameter and must be of the same type, for example, the following code is of type int

Note 2

A function cannot have multiple variable parameters at the same time

Note 3

The value of the variable parameter can also not be passed, but it is not recommended to do so

code:

namespace ParamsTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            PrintNumbers("fadf");

            Console.ReadKey();
        }

        static void PrintNumbers(string name, params int[] numbers)
        {
            Console.WriteLine(name);
            foreach (int number in numbers)
            {
                Console.WriteLine(number);
            }
        }
    }
}

run:

3. Simulate Console.WriteLine 

Console.WriteLine is a commonly used printing function in C#. It is basically used every day. Everyone is familiar with this.

Console.WriteLine("姓名:{0},年龄:{1}", "jack", 33);

Now use the params variable parameter to simulate such an effect, the code:

namespace ParamsTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Print("姓名:{0},年龄:{1}", "jack", 33);

            Console.ReadKey();
        }

        static void Print(string content, params object[] objects)
        {
            string printContent = string.Format(content, objects);
            Console.WriteLine(printContent);
        }
    }
}

run:

You may think, who can’t use string.Format, what’s your skill, it’s very simple!

It's really simple. In fact, Microsoft wrote it that way. I just copied it, hahaha. . .

 

 

Finish

If this post is helpful to you, please follow + like + leave a message, thank you!

end

Guess you like

Origin blog.csdn.net/qq_38693757/article/details/131439833