943,565 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 5686
  • C# RSS
Feb 7th, 2008
0

Array as Argument

Expand Post »
How do you pass an array of integers as an argument to a method in C#?
Reputation Points: 17
Solved Threads: 0
Junior Poster
complete is offline Offline
147 posts
since Dec 2005
Feb 7th, 2008
0

Re: Array as Argument

public void YourMethod(int[] arrOfInts)
{
//your code goes here..
}
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Dec 14th, 2009
-1
Re: Array as Argument
Click to Expand / Collapse  Quote originally posted by complete ...
How do you pass an array of integers as an argument to a method in C#?
C# Syntax (Toggle Plain Text)
  1. void ShowNumbers (params int[] numbers)
  2. {
  3. foreach (int x in numbers)
  4. {
  5. Console.Write (x+" ");
  6. }
  7. Console.WriteLine();
  8. }
  9.  
  10. ...
  11.  
  12. int[] x = {1, 2, 3};
  13. ShowNumbers (x);
  14. ShowNumbers (4, 5);
Last edited by Nick Evan; Dec 15th, 2009 at 3:45 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aravindsp is offline Offline
1 posts
since Dec 2009
Dec 14th, 2009
0
Re: Array as Argument
Note that the params keyword is not required unless you are going to add individual ints as multiple parameters to the method.
Reputation Points: 34
Solved Threads: 21
Junior Poster in Training
privatevoid is offline Offline
92 posts
since Feb 2009
Sep 7th, 2011
-1
Re: Array as Argument
following example show how to pass array as arguement...seee this

C# Syntax (Toggle Plain Text)
  1. private void ShowMessagebox(string[] arr)
  2. {
  3. string messageStr = "";
  4. for (int i = 0; i < arr.Length; i++)
  5. {
  6. messageStr += arr[i] + " ";
  7. }
  8. MessageBox.Show(messageStr);
  9. }
for more information visit AuthorCode: http://www.authorcode.com/how-to-pas...rguments-in-c/
Last edited by adatapost; Sep 7th, 2011 at 4:57 am. Reason: Added [code] tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hscoder is offline Offline
5 posts
since Sep 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: using array store elements and access by other button in c#.net
Next Thread in C# Forum Timeline: Add App.Config file in c#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC