Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 8 of 8

Thread: passing arrays in C#

  1. #1

    passing arrays in C#

    How do I pass an array of any length in C#?

  2. #2
    into a method?

  3. #3
    a function, yes.

  4. #4
    Code:
            static void Main(string[] args)
            {
                int[] numb = new int[2];
                numb[0] = 3;
                numb[1] = 9;
    
                displayArray(numb);
    
                Console.WriteLine("Method Over");
                Console.ReadKey(true);
            }
    
            
    
            static void displayArray(int[] arrayParam)
            {
                for (int i = 0; i < arrayParam.Length; i++)
                {
                    Console.WriteLine(arrayParam[i]);
                }
            }
    This an example. That what you were after?

  5. #5
    jsauni, don't ya love it when you go through the effort to help and they don't even bother to say thanks!

  6. #6
    HAHA, well I feel much better now that someone's at least seen my answer.

  7. #7
    well, the biggest question is why dont you use like List<int>? to me it just seems more logical and easier to read. Then you can also pass it like a normal variable.

    public void fun (List<int> myList)

  8. #8
    Be careful of using generic when it isn't necessary. If you do not need to be able to add to the array a new element, or sort, etc then the overhead included with generics is meaningless, and so int[] is a better option. However, if the ability of sorting, adding, deleting of elements in the array is necessary, I agree a generic makes sense.

    The memory footprint isn't huge, but based on the size of the array, it could be.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012