Hi people I found I can only reset the values in the array but I can resize it in a new method and return it back to main method,

static void Main()
        {
            int[] myArray = new int[4];
            setLength(myArray, 8);
        }

        static void setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

This is the code what I wrote, it works at the end of the setLength method,
but after it turns back to the main mathod, the array returns to the original one...

Could someone help me please?

You didnt set the return type to the setLenght method. You have to change the code to:

static void Main()
        {
            int[] myArray = new int[4];
            myArray = setLength(myArray, 8);
        }

        static int[] setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
            return myArray;
        }

or you can use ref keyword, to return, but I would suggest you to use upper example:

static void Main()
        {
            int[] myArray = new int[4];
            setLength(ref myArray, 8);
        }

        static void setLength(ref int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

Edited 5 Years Ago by Mitja Bonca: Added "return" keyword in the setLenght method

You didnt set the return type to the setLenght method. You have to change the code to:

static void Main()
        {
            int[] myArray = new int[4];
            myArray = setLength(myArray, 8);
        }

        static int[] setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

or you can use ref keyword, to return, but I would suggest you to use upper example:

static void Main()
        {
            int[] myArray = new int[4];
            setLength(ref myArray, 8);
        }

        static void setLength(ref int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
        }

Thanks so much man, the second solution works
lol I forgot "ref"...

Actually I tried your first solution before, but it didn't work,
the array still return to the original one after it goes back to main method...

anyway, thanks so much

Edited 5 Years Ago by x69chen: n/a

UPS, so sorry.. I guess Im already asleeping here.
I forgot a return keyword:

static void Main()
        {
            int[] myArray = new int[4];
            myArray = setLength(myArray, 8);
        }

        static int[] setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
            return myArray;
        }

This will do the trick mate.
good night.

UPS, so sorry.. I guess Im already asleeping here.
I forgot a return keyword:

static void Main()
        {
            int[] myArray = new int[4];
            myArray = setLength(myArray, 8);
        }

        static int[] setLength(int[] myArray, int length)
        {
            Array.Resize(ref myArray, length);
            return myArray;
        }

This will do the trick mate.
good night.

Yea this is what I tried before,
you can try to run it and it really don't work...
anyway, thanks so much for help and good night.

This article has been dead for over six months. Start a new discussion instead.