5
Contributors
4
Replies
6
Views
10 Years
Discussion Span
Last Post by hscoder
-1

How do you pass an array of integers as an argument to a method in C#?

void ShowNumbers (params int[] numbers)
{
    foreach (int x in numbers)
    {
        Console.Write (x+" ");
    }
    Console.WriteLine();
}

...

int[] x = {1, 2, 3};
ShowNumbers (x);
ShowNumbers (4, 5);

Edited by Nick Evan: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

0

Note that the params keyword is not required unless you are going to add individual ints as multiple parameters to the method.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.