C# Params

HTML
CSS
C#
SQL

Params

The array is a defined list of values for example if you have been told to buy apples, oranges & bananas from a supermarket then we can define an array called fruit {apple, orange, banana}. The array always has a defined value.

 If you have been told to buy fruits for 100 rupees then you will have to find out which fruits you can buy based on your budget. In this scenario we do not know which fruits you will be buying hence you cannot define the array. Params are used to define the list of values that depends on the output of another function

In C#, the params keyword is a modifier that allows developers to pass a variable number of parameters to a method or function. The params keyword is used to indicate that the method’s parameter list is treated as an array.

Params Keyword: It is useful when programmers don’t have any prior knowledge about the number of parameters to be used

Basic structure:

// Define a method using the params keyword
    static void MethodName(params Type[] parameterName)
    {
        // Method implementation goes here
    }

Scenario 1: We want to add 3 sets of numbers: sum1, sum2, and sum3. Assuming that we don’t know how many numbers there are in the set, we will use the params keyword in the below example.

Write a C# program where you create a custom function named Sum which takes integer parameters using the params keyword. Then call the custom function Sum in the Main method, passing different sets of integer values as parameters. Display the results to the console.

Example

using System;
class Program
{
    static void Main()
    {
        int sum1 = Sum(20,30,75);
        int sum2 = Sum(45, 50);
        int sum3 = Sum(10);
        Console.WriteLine(“Sum1: ” + sum1);
    }
    static int Sum(params int[] numbers)
    {
        int sum = 0;
        foreach (int num in numbers)
        {
            sum += num;
        }
        return sum;
    }
}

Output:
Sum1: 125

Sum2: 95
Sum3: 10

Explanation:

using System;
class Program
{
    static void Main()
    {
        int sum1 = Sum(20, 30, 75);

Calls the Sum method with the parameters 20, 30, and 75, and stores the result in integer variable sum1.

int sum2 = Sum(45, 50);

Calls the Sum method with the parameters 45 and 50, and stores the result in integer variable sum2.

int sum3 = Sum(10);

Calls the Sum method with the parameter 10, and stores the result in integer variable sum3.

Console.WriteLine(“Sum1: ” + sum1);

Prints the value of sum1 to the console, which is the sum of 20, 30, and 75.

Console.WriteLine(“Sum2: ” + sum2);

Prints the value of sum2 to the console, which is the sum of 45 and 50.

Console.WriteLine(“Sum3: ” + sum3);

Prints the value of sum3 to the console, which is the sum of 10.

static int Sum(params int[] numbers)

This defines a method named Sum that using the params keyword. The parameters are treated as an integer array named numbers.

int sum = 0;

Define a variable sum with value 0, which will hold the total sum of the numbers.

foreach (int num in numbers)

A foreach loop iterates over each integer in the numbers array.

sum += num;

In each iteration, the current number (num) is added to the sum variable.

return sum;

After the loop ends, the method returns the total value to the Sum method.

Scenario 2: We want to add 1 set of values. If we know how many numbers there are in the set, we will use the Array in the below example.

Implement a custom method named Sum that takes an array of integers as a parameter and returns the sum of its elements. The Main method should create an array of integers named numbers, call the Sum method with this array, and display the result in the console

Example

using System;
class Program
{
    static void Main()
    {
        int[] numbers = { 20, 30, 75, 45, 50, 10 };
        int sum1 = Sum(numbers);
       Console.WriteLine(“Sum1: ” + sum1);
    }
    static int Sum(int[] numbers)
    {
        int sum = 0;
        foreach (int num in numbers)
        {
            sum += num;
        }
        return sum;
    }
}

Output:
Sum1: 230

Explanation:

using System;
class Program
{
    static void Main()
    {
        int[] numbers = { 20, 30, 75, 45, 50, 10 };

Define an integer array named numbers and initialize it with values.

int sum1 = Sum(numbers);

Calls the Sum method with the numbers array as the argument and stores the result in integer variable sum1.

Console.WriteLine(“Sum1: ” + sum1);

Prints the value of sum1 to the console, which is the sum of all elements in the numbers array.

static int Sum(int[] numbers)

This defines a method named Sum that takes an integer array as a parameter.

int sum = 0;

Define an integer variable sum with value 0, which will hold the total sum of the numbers.

foreach (int num in numbers)

A foreach loop iterates over each integer in the numbers array.

sum += num;

A foreach loop iterates over each integer in the numbers array.

return sum;

After the loop ends, the method returns the total value to the Sum method.

Task:

Sum of Variable Number of Integers:
Create a function SumParams that takes a params array of integers and returns the sum of its elements.
Test the function by passing different numbers of integers and printing the result.

Concatenate Strings:
Create a function ConcatenateParams that takes a params array of strings and returns a single concatenated string.
Test the function by passing different numbers of strings and printing the result.

Find Maximum Value:
Create a function FindMaxParams that takes a params array of integers and returns the maximum value.
Test the function by passing different numbers of integers and printing the result.

Average of Variable Number of Doubles:
Create a function AverageParams that takes a params array of doubles and returns the average of its elements.
Test the function by passing different numbers of doubles and printing the result.

Product of Variable Number of Integers:
Create a function ProductParams that takes a params array of integers and returns the product of its elements.
Test the function by passing different numbers of integers and printing the result.

Print Variable Number of Elements:
Create a function PrintParams that takes a params array of any type (use params object[]) and prints each element.
Test the function by passing different numbers and types of elements and printing the result.