.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Parameter Arrays in C#

Posted By:Kirtan       Posted Date: July 17, 2010    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  

Parameter Arrays in C#. In this article we will see how to use Parameter Arrays with an example.
 

Parameter Arrays

In normal function we can allow fixed number of function arguments like

Int Add(int x,int y )
{
      Return x+y;
}

But what if number of augments are not fixed we want that user allowed to pass as much arguments he want to entered are allowed .In this case the parameter arrays comes in handy..

We can define param array in Function argument by keyword "params" followed by and then array name.

We will make a add function that will allow as many arguments as user wants to input in function

class Program
{
        static int Add(params int[] nums)
        {
            int total=0;
            foreach(int i in nums)
            {
                total = total+i;
            }
            return total;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Parameter Array Function Testing ...");

            int result=0;

            /* function allowing 3 arguments */
            result = Add(10, 10, 10);
            Console.WriteLine("Result for 3 Prameter :{0}", result);

            /* function allowing 4 arguments */
            result = Add(10, 10, 10, 10);

            Console.WriteLine("Result for 4 Prameter :{0}", result);

            /* function allowing 5 arguments */
            result = Add(10, 10, 10, 10,10);

            Console.WriteLine("Result for 5 Prameter :{0}", result);


            /* function is also allowing whole array too */
            int[] x = { 10, 10, 10, 10, 10, 10, 10, 10 };

            Console.ForegroundColor = ConsoleColor.Red;
            result = Add(x);
            Console.WriteLine("Result for Array Summation Prameter :{0}", result);

            Console.ReadKey();
}




 Subscribe to Articles

     

Further Readings:

Responses
Author: rajendra         Company URL: http://www.dotnetspark.com
Posted Date: July 27, 2010

sitemaps are latest versions usedin master pages


Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend