Solved

Optional Arguments

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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…

789 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