SIGN UP MEMBER LOGIN:    
ARTICLE

Parameter Arrays in C#

Posted by Kirtan Patel Articles | Current Affairs July 19, 2010
In this article you will learn how to use Parameter Arrays in C#.
Reader Level:
Download Files:
 

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 <type name> 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();
        }


image1.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor