1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

passing arrays in C#

Discussion in 'C#' started by Xarzu, Feb 7, 2008.

  1. Xarzu

    Xarzu New Member

    Joined:
    Dec 18, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    How would I pass an array of integers (of unknown lenght) as an argument to a function in C#?
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    Array of integers should do the job.
     
  3. Statement

    Statement New Member

    Joined:
    Mar 12, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create and initialize some array...
                int[] myArray = new int[10];
    
                for (int i = 0; i < 10; ++i)
                {
                    myArray[i] = i * 10;
                }
    
                // Use it.. 
                PassArray(myArray);
            }
                                        //v---------- This is where the "magic" happens. Declare the type as 'int[]'.
            static void PassArray(int[] array)
            {
                foreach (int i in array)
                {
                    Console.WriteLine(i);
                }
            }
        }
    }
    
     
  4. Lihualee

    Lihualee New Member

    Joined:
    Sep 19, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    yes!

    bump up then lurk
     

Share This Page