Tom
Tom

Reputation: 577

c# Is it possible to recreate an array?

I have a class, CircularBuffer, which has a method CreateBuffer. The class does a number of things but occasionally I need to change the size of an array that is used in the class. I do not need the data any longer. Here is the class:

    static class CircularBuffer
    {
        static Array[,] buffer;
        static int columns, rows;

        public static void CreateBuffer(int columns, int rows)
        {
            buffer = new Array[rows,columns];
        }   

        //other methods that use the buffer
    }

Now the size of the buffer is up to 100 x 2048 floats. Is this going to cause any memory issues, or will it be automatically replaced with no issues? Thanks

Upvotes: 1

Views: 468

Answers (1)

InBetween
InBetween

Reputation: 32750

You are, technically speaking, not recreating anything. You are simply creating a new array and overwriting the variable's value (the address, so to speak, of the array its referencing).

It's important therefore that you distinguish what you are really replacing; you are not replacing the array, only the reference to it.

Problems? None. By your code, the old array will not be reachable anymore and will therefore be eligible for collection by the GC. If the collection ever happens is up to the GC but its not something you should worry about.

Upvotes: 2

Related Questions