Use the params keyword in C#

[params keyword]

If you use the params keyword before an array parameter used as the last parameter in a method, then the calling code can pass any number of values for that parameter. The following code defines a ShowValues method that can take any number of string parameters.

// Display zero or more values.
private void ShowValues(params string[] values)
    foreach (string value in values)

The following code shows how the example program calls ShowValues passing it 0, 3, or 5 parameters.

private void btn0_Click(object sender, EventArgs e)

private void btn3_Click(object sender, EventArgs e)
    ShowValues("Red", "Green", "Blue");

private void btn5_Click(object sender, EventArgs e)
    ShowValues("Aardvark", "Bear", "Cantalope", "Dingo", "Eagle");

That’s all there is to it.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in methods and tagged , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Use the params keyword in C#

  1. brave_girl says:

    how to add many icons from form1 with different distance (2cm) in another form2?

    • RodStephens says:

      I’m not sure I understand your question, but here’s a guess. On Form1, you have a list of icons with checkboxes. You check some and press a button. Then another form appears showing the selected icons. Here’s how you could do that.

      Add CheckBox controls to the first form. Set their Text properties to “” and set their Image properties to the images you want to display.

      When the user presses a button, create an instance of the new form. Then check each of the CheckBoxes. If one is checked, create a new PictureBox, set its Image property equal to the CheckBox’s Image property, and add the PictureBox to the new form. You’ll need to use x and y variables to keep track of the position of the PictureBox on the new form so you can arrange the PictureBoxes nicely.

      That’s the basic idea. To be fancy, you can make an array holding the CheckBoxes on the first form and then loop through them to make this easier.

      • brave_girl says:


        It is almost look like that but in there is a separate tabcontrol tab1 is for the menu list (from which we will add icons) and the tab2 is the table of check box without button inside ( from which we will draw the line between icons ) and it is shown in another tabcontrol tab3( it is the one who was invisible then become visible when we finish checked each of the check boxes).

        I hope my question is now clear for you.

        Thanks dude!

        • RodStephens says:

          For each icon you click, create a new PictureBox and set its Image property equal to the icon’s image. Then set the new PictureBox’s Parent property equal to the TabControl’s TabPage entry where you want the new icon to appear.

          Set the TabControl (or whatever is containing it) Visible property to true to make it appear.

Leave a Reply

Your email address will not be published. Required fields are marked *