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

Recommended Answers

All 4 Replies

public void YourMethod([b]int[] arrOfInts[/b])
{
//your code goes here..
}

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);

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

following example show how to pass array as arguement...seee this

private void ShowMessagebox(string[] arr)
            {
                string messageStr = "";
                for (int i = 0; i < arr.Length; i++)
                {
                    messageStr += arr[i] + " ";
                }
                MessageBox.Show(messageStr);
            }

for more information visit AuthorCode: http://www.authorcode.com/how-to-pass-arrays-as-arguments-in-c/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.