C# Params

Params

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 don’t know how many fruits you’ll need in advance, so you can use params to handle any number of items.

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.

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: Imagine your school is organizing a picnic for 9th-grade students. You need to list the students’ names from different sections (9th-A and 9th-B) and count how many students are in each section. But the issue is you don’t know how many students will attend the picnic, and you get the names from different sections. We use params to handle this.

Example

using System;
class Program
{
       static void Main()
      {

        Console.WriteLine(“Students names for Picnic”);

        Console.WriteLine(“9th – A Class Students Name”);
        string Ninth_A = Ninth(“Umar”, “Sohail”, “Saqlain”, “Hamza”);
        Console.WriteLine(“Count of students from class 9th-A – ” + Ninth_A);

        Console.WriteLine(“9th – B Class Students Name”);
        string Ninth_B = Ninth(“Sameer”, “Dinesh”, “Dipak”);
        Console.WriteLine(“Count of students from class 9th-B – ” + Ninth_B);
        Console.ReadLine();
       }
          static string Ninth(params string[] students)
      {
         int studentcount = 0;
        foreach (string names in students)
          {
             Console.WriteLine(names);
             studentcount++;
          }
return studentcount.ToString();
     }
}

Output:

Students names for Picnic
9th – A Class Students Name
Umar
Sohail
Saqlain
Hamza
Count of students from class 9th-A – 4
9th – B Class Students Name
Sameer
Dinesh
Dipak
Count of students from class 9th-B – 3

Explanation:

using System;
class Program
{
    static void Main()
    {

This line prints the text “Students names for Picnic” on the console screen.

Console.WriteLine(“Students names for Picnic”);

This line prints “9th – A Class Students Name” on the console screen.

Console.WriteLine(“9th – A Class Students Name”);

Here, we are calling the ‘Ninth’ function and passing 4 students’ names of Ninth-A. It will count how many names are given, and return that number as text.

string Ninth_A = Ninth(“Umar”, “Sohail”, “Saqlain”, “Hamza”);

This line prints the number of students in 9th-A class on the console screen.

Console.WriteLine(“Count of students from class 9th-A – ” + Ninth_A);

This line prints “9th – B Class Students Name” on the console screen

Console.WriteLine(“9th – B Class Students Name”);

Here, we are calling the ‘Ninth’ function again, but this time with 3 students’ names of Ninth-B. It will count the names and return that number as text.

string Ninth_B = Ninth(“Sameer”, “Dinesh”, “Dipak”);

This line prints the number of students in 9th-B class on the console screen.

Console.WriteLine(“Count of students from class 9th-B – ” + Ninth_B);

This makes the program wait for the user to press Enter before it closes

Console.ReadLine();
}

This is the ‘Ninth’ function, it can take many names and count them.

static string Ninth(params string[] students)
{

We start with the count of students as 0.

int studentcount = 0;

This loop will go through each student name one by one.

foreach (string names in students)
{

It shows each student’s name on the screen.

Console.WriteLine(names);

Every time we see a name, we add 1 to the count.

studentcount++;
}

Finally, we return the total count of students as text.

    return studentcount.ToString();
   }
}

Scenario 2 – Imagine your school is organizing a picnic for 9th-grade students. You need to list the students’ names from different sections (9th-A and 9th-B) and count how many students are in each section. But now you know the names of student and count so we will use array which is define list of value.

Example

using System;
class Program
 {
       static void Main()
      {
          Console.WriteLine(“Students names for Picnic”);

          Console.WriteLine(“9th – A Class Students Name”);
          string[] studentnames_of_9th_A = { “Umar”, “Sohail”, “Saqlain”, “Hamza” };
          string Ninth_A = Ninth(studentnames_of_9th_A);
          Console.WriteLine(“Count of students from class 9th-A – ” + Ninth_A);

         Console.ReadLine();
      }
static string Ninth(string[] students)
   {
       int studentcount = 0;
       foreach (string names in students)
      {
         Console.WriteLine(names);
         studentcount++;
      }
      return studentcount.ToString();

     }
}

Output:

Students names for Picnic
9th – A Class Students Name
Umar
Sohail
Saqlain
Hamza
Count of students from class 9th-A – 4

Explanation:

using System;
class Program
{
      static void Main()
       {

This prints text – “Students names for Picnic” on console.

Console.WriteLine(“Students names for Picnic”);

This prints text – “9th – A Class Students Name” on console

Console.WriteLine(“9th – A Class Students Name”);

This creates an string array with the variable – studentnames_of_9th_A and value – Umar, Sohail, Saqlain, and Hamza

string[] studentnames_of_9th_A = { “Umar”, “Sohail”, “Saqlain”, “Hamza” };

This calls the function Ninth and stores the number of students in Ninth_A variable.

string Ninth_A = Ninth(studentnames_of_9th_A);

This prints the number of students of 9th-A.

Console.WriteLine(“Count of students from class 9th-A – ” + Ninth_A);

This keeps the program open until you press Enter.

    Console.ReadLine();
    }

This defines the function Ninth that counts student names.

static string Ninth(string[] students)
{

This sets the starting count of students to 0.

int studentcount = 0;

This loop goes through each student’s name.

foreach (string names in students)
{

This prints each name.

Console.WriteLine(names);

This adds 1 to the student count each time a name is printed

 studentcount++;

 This converts the student count to a string and sends it back.

     return studentcount.ToString();
    }
}

Task:

1. 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.

2. 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.

3. 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.

4. 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.

5. 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.

6. 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.