Solved

Optional Arguments

Posted on 2004-08-17
3
199 Views
Last Modified: 2010-04-17
Dear Friends,
I am new to C#.Can anybody tell me how to pass optional arguments while calling a function in C#.Also, what is the use of "param"  keyword?
0
Comment
Question by:Dharmen-Patel
3 Comments
 

Accepted Solution

by:
mjdmjd earned 50 total points
ID: 11819529
The param thing is for your comments - see this on the MS website

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfparam.asp

As for the optional arguments, you can either overload the method i.e.

public void foo (string mystr)
{
}

public void foo (string mystr, bool mybool)
{
}

or use the params keyword (which is maybe what you meant instead of param in your origional question)

   public static void UseParams(params int[] list)
   {
      for ( int i = 0 ; i < list.Length ; i++ )
         Console.WriteLine(list[i]);
      Console.WriteLine();
   }

You can then call this function with any number of integers like this

useparams(1)
useparamts(1,2,3,4,5,6)
etc ...

only thing to watch out for is that the params keyword has to come after any other paramaters.

hope this helps
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 106
java  and programming certification ? 4 94
This code tracks birthdays 3 73
iframe detection of parent window scale 20 72
This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question