C# | Params

Params is an important keyword in C#. It is used as a parameter which can take the variable number of arguments.

Important Point About Params Keyword :

  • It is useful when programmer don’t have any prior knowledge about the number of parameters to be used.
  • Only one Params keyword is allowed and no additional Params will be allowed in function declaration after a params keyword.
  • The length of params will be zero if no arguments will be passed.

Examples: To illustrate the use of params keyword

  • Simple program to show the params keyword usage
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the 
    // use of params keyword
    using System;
    namespace Examples {
          
    class Geeks {
          
        // function containing params parameters
        public static int Add(params int[] ListNumbers)
        {
            int total = 0;
              
            // foreach loop
            foreach(int i in ListNumbers) 
            {
                total += i;
            }
            return total;
        }
              
    // Driver Code    
    static void Main(string[] args)
    {
          
        // Calling function by passing 5
        // arguments as follows
        int y = Add(12,13,10,15,56);
          
        // Displaying result
        Console.WriteLine(y);
    }
    }
    }

    chevron_right

    
    

    Output :

    106
    

    Explanation : There is no need to define the size of the array because using params keyword in above program, the Integer data will be in the form:
    ListNumbers
    [0] 12
    [1] 13
    [2] 10
    [3] 15
    [4] 56

  • Object type Params will allow any type of arguments and any number of arguments as follows :
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the 
    // use of object type params
    using System; 
      
    namespace Example2 {
    class Geeks {
          
            // function using object type params 
            public void result(params object[] array) 
            
                for (int i = 0; i < array.Length; i++) 
                
                      
                    // Display result
                    Console.WriteLine(array[i]); 
                }     
            
      
        // Driver Code 
        static void Main(string[] args) 
        
            Geeks gfg = new Geeks(); 
              
            // Variable length arguments
            gfg.result("Geeks", "GFG",
                       "ProGeek Cup 2.0",
                            "G4G", "100");
        
      

    chevron_right

    
    

    Output :

    Geeks
    GFG
    ProGeek Cup 2.0
    G4G
    100
    

    Explanation : In above program the object type params parameter can accept any type of data and any number of arguments.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.