C# Params - Params Keyword in C#



C# Params - Params Keyword in C#

csharp-params
  • In C#, params is a keyword which is used to specify a parameter that takes variable number of arguments.
  • It is useful when we don't know the number of arguments prior.
  • Only one params keyword is allowed and no additional parameter is permitted after params keyword in a function declaration.

Sample Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Params
{
    class Program
    {
        
        public void Show(params int[] val) 
        {
            for (int i = 0; i < val.Length; i++)
            {
                Console.WriteLine(val[i]);
                
            }
        }
        // Main function, execution entry point of the program  
        static void Main(string[] args)
        {
            Program program = new Program(); // Creating Object  
            program.Show(2, 4, 6, 8, 10, 12, 14); // Passing arguments of variable length  
            Console.ReadLine();
        }
    }
}  

Output

2
4
6
8
10
12
14
        

Related Searches to C# Params - Params Keyword in C#