C# Keywords Tutorial Part 63: params

C# Keywords Tutorial Part 63: params

C# is a versatile programming language that allows developers to create powerful and efficient applications. One of the language’s features is the “params” keyword, which enables developers to pass a variable number of parameters to a method or function. In this blog post, we’ll take a closer look at the “params” keyword in C# and provide some code examples to help illustrate its functionality.

What is the “params” Keyword in C#?

The “params” keyword is a modifier that can be used in a method or function’s parameter list to indicate that the method accepts a variable number of arguments. When this keyword is used, the method’s parameter list is treated as an array, and any number of arguments can be passed to the method. The “params” keyword is used to make a method more flexible and easier to use, as it allows for a variable number of arguments to be passed in.

Syntax of “params” Keyword

The syntax of the “params” keyword is simple. You simply add the “params” keyword before the last parameter of the method. Here’s an example:

public void DoSomething(params int[] values)
{
    // Do something with the values
}

In this example, the “DoSomething” method takes a variable number of integer values as arguments. The “params” keyword is used to indicate that the method’s parameter list can accept any number of integer values.

To call this method and pass in integer values, you can do the following:

DoSomething(1, 2, 3, 4);
//OR
int[] values = { 1, 2, 3, 4 };
DoSomething(values);

Using the “params” Keyword in C#

Now that we’ve seen the syntax of the “params” keyword, let’s take a look at how it can be used in practice. Here are a few examples:

Example 1: Finding the Sum of Multiple Integers

Suppose you want to write a method that finds the sum of multiple integers. Using the “params” keyword, you can create a method that accepts any number of integer arguments and returns their sum. Here’s the code:

public int Sum(params int[] values)
{
    int sum = 0;
    foreach (int value in values)
    {
        sum += value;
    }
    return sum;
}

In this example, the “Sum” method takes a variable number of integer values as arguments. The method then uses a foreach loop to iterate through the values and add them together to find the sum.

Example 2: Creating a Concatenated String

Suppose you want to write a method that concatenates a variable number of strings into a single string. Using the “params” keyword, you can create a method that accepts any number of string arguments and returns a concatenated string. Here’s the code:

public string Concatenate(params string[] values)
{
    return string.Concat(values);
}

In this example, the “Concatenate” method takes a variable number of string values as arguments. The method then uses the “string.Concat” method to concatenate the values into a single string and return the result.

Example 3: Passing a Mix of Fixed and Variable Arguments

Suppose you have a method that takes a mix of fixed and variable arguments. Using the “params” keyword, you can create a method that accepts both fixed and variable arguments. Here’s the code:

public void DoSomething(int fixedValue, params int[] variableValues)
{
    // Do something with the fixedValue and variableValues
}

In this example, the “DoSomething” method takes an integer fixedValue as a fixed argument and a variable number of integer variableValues as arguments.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *