C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Using Params in C#


Posted Date: 02 Mar 2004    Resource Type: Articles    Category: .NET Framework
Author: SasitharMember Level: Silver    
Rating: 1 out of 5Points: 7



Some times we may require a method which can accept variable number of arguments.

C# provides the params keyword for this purpose.
The syntax for using params keyword is

params datatype[] argument name

For using the params keyword the argument must be declared as single dimensional array.Once an argument is prefixed with the params keyword, C# will accept any number of values (including none)for that argument.

Example


public class Test
{
public int Sum(params int[] num)
{
int totval = 0;
foreach (int n in num)
{
totval += n;
}

return totval;
}

public static int Main(string[] args)
{
Test T = new Test();

Console.WriteLine(T.Sum());
Console.WriteLine(T.Sum(3,2));
Console.WriteLine(T.Sum(50,60,100,150));
return 0;
}
}



The output of the above program will be
0
5
360




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: C# Tip on 'Using'
Previous Resource: Intranet messenger
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use