Parameter Arrays in C#

No.of Views1605
Bookmarked0 times
Downloads 
Votes0
By  kirtan007   On  17 Jul 2010 10:07:24
Tag : CSharp , Miscellaneous
In normal function we can allow fixed number of function arguments
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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.

Implementation

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

 Output

Image Loading

Sample Project Source

Download source files -24 kb

 
Sign Up to vote for this article
 
About Author
 
kirtan007
Occupation-
Company-
Member Type-Senior
Location-Not Provided
Joined date-02 Jul 2009
Home Page-http://kirtan.uni.cc
Blog Page-
He completed his Bachelor of Computer Application from Gujarat University 2009 .He is doing Master of Computer Application from Gujarat Technological University right now .. His area of Interests are Web Hacking , C# .net Windows form ,asp.net , WPF ,Silverlight ,SQL Server and Some PHP.
 
 
Other popularSectionarticles
Comments
By:dddddDate Of Posted:8/8/2010 8:13:13 AM
cmment
Using dynamic parameters have some limitations, such as params type array should appear in the last arugment.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top