Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Params in C#
3.
Example to demonstrate the use of params keyword in C#
3.1.
Output
4.
Using params keyword in C# for any datatype
4.1.
Output
5.
FAQs
5.1.
What is C#?
5.2.
What is the params keyword in C#?
5.3.
Is it possible to pass zero variables to a params argument?
5.4.
What is the main function?
5.5.
What type of variable should be defined in the function definition to pass any type and number of arguments?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Params in C#

Author Teesha Goyal
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

C# is a simple, high-level, general-purpose language. It supports the Object-oriented programming paradigm.  Microsoft developed it under the .NET platform. It is much similar to C/C++. C# can be used for web development, web services, and desktop application development.

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable.

Params in C#

Params is a keyword used in C#. It specifies a variable number of parameters or arguments to a function. It is mainly used in the functions where we don’t know the number of parameters passed to it or in functions where the number of parameters depends on the user. 

We can use the params keyword only once in a function; no other params keywords can be used after one it is used once.

If no arguments are passed, the params variable will have zero length and no values. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example to demonstrate the use of params keyword in C#

/*program to demonstrate the use of params keyword for integer array in C# */
using System;
namespace Example {
class Demo {
     
    /* function that has the params keyword */
    public static int Add_Numbers(params int[] Numbers)
    {
        int sum = 0;
       
        /* using foreach loop to sum the Numbers */
        foreach(int i in Numbers)
            sum += i;
       
        return sum;
    }
         
    /* Driver function */
    static void Main(string[] args)
    {
        /* Calling Add_Numbers functions with variable arguments */
        int temp = Add_Numbers(98,76,99,23,44);
        int temp2 = Add_Numbers(23,96);
       
        /* passing zero arguments */
        int temp3 = Add_Numbers();
     
        /* Printing returned values */
        Console.WriteLine(temp);
        Console.WriteLine(temp2);
        Console.WriteLine(temp3);
    }
}
}

Output

340
119
0

Using params keyword in C# for any datatype

To pass a variable number of arguments of any datatype in C# using the params keyword, we must specify the data type as an object. The params object argument will take in any number of arguments and data types. To learn more about C# arrays, visit C# Arrays

/* program to demonstrate the use of params keyword for any data type */
using System;
namespace Example {
class Demo {
     
    /* function that has the params keyword with object data type */
    public void Print_Numbers(params object[] List_of_objects)
    {
        /* using for loop to print each element in List_of_objects */
        for (int i = 0; i < List_of_objects.Length; i++)
            {
                Console.WriteLine(List_of_objects[i]);
            }
    }

    /* Driver function */
    static void Main(string[] args)
    {
        Demo obj = new Demo();
        /* Calling Print_Numbers functions with variable arguments */
        obj.Print_Numbers("example_argument", 101.5, 90, "hello", "world");
        obj.Print_Numbers("demo", 26);
       
        /* passing zero arguments */
        obj.Print_Numbers();
    }
}
}

Output

example_argument
101.5
90
hello 
world
demo
26

FAQs

What is C#?

C# is a simple, high-level, general-purpose language. It supports the Object-oriented programming paradigm. Microsoft developed it under the .NET platform.

What is the params keyword in C#?

Params is a keyword used in C#. It specifies a variable number of parameters or arguments to a function. 

Is it possible to pass zero variables to a params argument?

Yes. If no arguments are passed, the params variable will have zero length and no values. 

What is the main function?

The main function is the driver function. It defines the starting point from which the execution of the program begins. 

What type of variable should be defined in the function definition to pass any type and number of arguments?

If we use the params keyword, we can pass any number of variables, and when we define the argument as object type, then any data type can be passed to it.

Conclusion

This article discussed the params keyword in the C# programming language. We discussed using the params keyword for any specific or object data type. To learn C# visit our site C# | Learn and Practice. I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass