954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array as Argument

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

complete
Junior Poster
153 posts since Dec 2005
Reputation Points: 17
Solved Threads: 0
 
public void YourMethod(<strong>int[] arrOfInts</strong>)
{
//your code goes here..
}
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 
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);
aravindsp
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

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

privatevoid
Junior Poster in Training
92 posts since Feb 2009
Reputation Points: 34
Solved Threads: 21
 

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/

hscoder
Newbie Poster
6 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Infraction Points: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You