Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an array in c#?

Tags:

arrays

c#

I'm sorry if this is an obvious question but neither Google or a search here led me to an answer.

Is there a way to remove an array entirely?

I want the opposite of int[] array = new int[5]

like image 926
Patrik Björklund Avatar asked Feb 24 '09 23:02

Patrik Björklund


People also ask

Can we delete element from array?

Java arrays do not provide a direct remove method to remove an element. In fact, we have already discussed that arrays in Java are static so the size of the arrays cannot change once they are instantiated. Thus we cannot delete an element and reduce the array size.

How do you remove an array from an array?

You can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically.


1 Answers

Say you call:

 void Foo(){      int[] a = new int[5];  } 

In C# there is no way to undefine the variable a. That means a will be defined in Foo even if you set a to null. However, at the end of Foo a will fall out of scope. That means no code can reference it, and the garbage collector will take care of freeing the memory for you the next time it runs, which might not be for a long time.

like image 89
RossFabricant Avatar answered Oct 02 '22 00:10

RossFabricant