Solved

Optional Arguments

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 132
VBSCRIPT string remove all characters to the right after extension 4 71
Help Required 2 46
Generate Unique ID in VB.NET 21 99
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

685 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