Params Keyword in C# With Examples

The params keyword in C# is used to specify a method parameter that can take a variable number of arguments. Generally, params work as a single-dimensional array of objects.

C# params are useful when you declare a method and don’t know how many arguments will be passed as parameters.

Params keyword in CSharp

Important Points About Params Keyword :

  • params keyword is useful when the developer doesn’t know in advance about the number of parameters to be used.
  • The params parameter type must be declared as an array. Example: MyMethod(params int[] arr);
  • Only one params keyword is allowed to be declared as a method parameter.
  • The length of params will become zero if no arguments are passed.
  • A params parameter must be the last argument in the method’s parameter list. Example: MyMethod( int a, int b, params int[] arr)
  • params cannot have a default value assigned. Example: params int[] arr= 10;
  • The params parameter accepts a comma-separated list of arguments of the type of the array elements.
  • params keyword can’t be used with the combination of ref and out.

C# Params Keyword Example

The following code demonstrates how to use the params keyword in C# to specify method parameters that accept multiple arguments.

Here, we have created a method called AddNumbers with the params parameter and called it with an array of numbers. This method sums all the numbers passed as parameter and returns the total sum as the result.

// C# program to demonstrate the params as a method parameter in C#
using System;
namespace Params_Keyword_Example
{
    class Program
    {
        // Params Parameter  
        public static int AddNumbers(params int[] arr)
        {
            int sum = 0;

            foreach (int num in arr)
            {
                sum += num;
            }
            return sum;
        }
        public static void Main(string[] args)
        {
            int total = AddNumbers(10, 20, 30, 40);
            Console.WriteLine($" Sum: {total}");
            Console.ReadLine();
            // Output:
            // Sum: 100
        }
    }
}

C# Params Keyword Example With Object Type

The following C# code example will show you how to pass different values to a method using params of object type.

class Program
    {
        // Params Parameter  
        public static void Print(params object[] arr)
        {          

            foreach (var item in arr)
            {
                Console.WriteLine($" {item} ,");
            }
        }

        public static void Main(string[] args)
        {
            Console.WriteLine($"*** Print params elements ***");
            Print("INDIA","USA","FRANCE", 10, 20.5);           
            Console.ReadLine();           
        }
    }

Output:

params keywords example
params keywords result

Can you give a good example of the params keyword?

A good example of using params is the String.Format() method. This method allows a user to pass in a string formatted to their requirements, as well as numbers of parameters for the values to insert into the string. You can imagine, If the params keyword did not exist, then the String.Format() would require an infinite number of overloads methods.

params in string.format class
Params keyword in String.Format class

FAQ

Q: What is the purpose of the params keyword?

The params keyword can be used to pass a variable number of parameters to a method that accepts an array.

Q: Can we define a params type as multi-dimensional array?

No, The params type must always be a single-dimensional array.

Q: Is it possible to have null params in C#?

Yes, the array associated with params can be null.

Q: What is params in C#?

In C#, params is a keyword that is used to specify a parameter that takes variable number of arguments. It can be used when you don’t know the number of arguments in advance to be passed in a function.

References: MSDN-params (C# Reference)

Conclusion

In this article, we discussed about the params keyword along with multiple example.
I hope you enjoyed and found this post useful about params keyword. If you have any questions or comments, please leave them in the comments section below.

Related articles:

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments