Open In App

C# Params

Last Updated : 11 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Params is an important keyword in C#. It is used as a parameter which can take the variable number of arguments of specific data type. It is a useful feature when the number of arguments is unknown, providing flexibility in method calls. Some key points involved with params in C# are mentioned below:

  • It is useful when programmers don’t know the number of parameters to be used.
  • Only one Params keyword is allowed and no additional Params will be allowed in function declaration after a params keyword.
  • The length of the params will be zero if no arguments are passed.

Example: Program to add the elements of array passed on the method

C#
// Using params keyword
using System;

class Geeks {

    // method accepting variable elements
  	// using params
    public static int Add(params int[] ListNumbers)
    {
        int total = 0;

        // foreach loop
        foreach(int i in ListNumbers) {
          	total += i; 
        }

        return total;
    }

    // Main Method
    static void Main(string[] args)
    {
        // Elements to accept in Method
        int y = Add(12, 13, 10, 15, 56);

        // Displaying result
        Console.WriteLine(y);
    }
}

Output
106

Rules

  • Single params per method: We can only use one params parameter in a method to avoid ambiguity. We need to specify the array type, and all arguments passed must be compatible with that type.
  • Params must be the last parameter: The params keyword should be the last parameter in the method signature to ensure that the method correctly interprets the arguments.
  • Empty array: If no arguments are passed to a params parameter, it will be treated as an empty array.

Example 2: Object type Params that allow any type of arguments and any number of arguments.

C#
// Using Object Type params
using System;

public class Geeks 
{
  	// Method with params
    public string ConcatString(params string[] words) {
        return string.Join(" ", words);
    }

  	// Main Method
    public static void Main()
    {
        Geeks sc = new Geeks();

        // Calling ConcatString with varying number of
        // arguments
        Console.WriteLine(sc.ConcatString("Hello", "Geek"));
      	Console.WriteLine(sc.ConcatString("This", "is", "a", "test"));
        Console.WriteLine(sc.ConcatString("One"));
      
        // without any argument
        Console.WriteLine(sc.ConcatString());
    }
}

Output
Hello Geek
This is a test
One

Explanation: Here params object[] accepts a variable number of arguments of any type (due to object[]). The resulting method is called with a mix of strings and an integer and shows the flexibility of object[] as the parameter type.

Benefits of Using Params

  • Simplifies Method Overloading: Without params, we need to create multiple method overloads to handle different numbers of arguments. This increases the complexity and reduces maintainability. Allows the single method to accept any number of arguments.
  • Improved Code Readability: It enhances code readability by eliminating the need to manually create arrays or use multiple overloads. Pass values directly to the method in a comma-separated list, making method calls more easier to read.
  • Flexibility with Argument Count: Allow to pass any number of arguments (including none), making the method more flexible. The method can handle any number of values, making it more versatile and adaptable.
  • Supports Implicit Array: It automatically wraps values in an array. This eliminates the need of explicitly create an array before passing it, improving ease of use.

Next Article
Article Tags :
Practice Tags :

Similar Reads