C# Params Array

params-array
In this chapter you will learn:

  • What is param array in C#?
  • What is the advantage of using param array?
  • How to use param array in C# programming?

Sometimes, you are not assured about number of parameters or you want to create a method that can accept n number of parameters at run time. This situation can be handled with params type array in C#. The params keyword creates an array at run time that receives and holds n number of parameters.

In the preceding line, the allnumber variable can holds n number of parameters at runtime because it is declared with params keyword.

Programming Example of Params Array in C#

In this example, we are creating a function add() that will receives any number of integer parameters at run time and returns the sum of all those number. We will use params array to achieve this goal in C#.

Output

Sum of 1,2,3 is:         6
Sum of 3,5,2,6,2 is:   18
__

 

Summary

In this chapter you learned about param array in C#. You also learned how to use param array in C# programming. In next chapter you will learn how to pass array as parameter.

 

More Examples

 


2 thoughts on “C# Params Array”

Leave a Reply

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