Solved

Optional Arguments

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Batch file output 20 78
copyEvens challenge 6 63
Fibonacci challenge 11 84
advertisement module in core php 4 92
A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

11 Experts available now in Live!

Get 1:1 Help Now