Solved

Optional Arguments

Posted on 2004-08-17
3
198 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is about my first experience with programming Arduino.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now