View Full Version : C#: how to empty an array?
Typhoon
04-20-2005, 01:13 AM
If I create an array:
object[] array = new object[100];
then would:
array = null;
kill it? And do I have to point array to anything?
OSUKid7
04-20-2005, 01:26 AM
From what I know in other languages, that should do the trick. (I don't know C#, but do know C++, Java, and primarily use php these days.)
bacchus_3
04-20-2005, 06:19 AM
My C# have been rusty, I've learned it when it was released but never got back due to change of platform. C# has a garbage collection process in the background much like that of java. It removes/destroys objects that are not referenced by any variable when the garbage collector is called.
** Just an extra
Note however that if you used the same object like this way,
object[] array = new object[100];
object[] array2 = array;
array=null;
would not destroy the object until array2 gets/is set to another object
manywhere
04-20-2005, 07:07 AM
If I create an array:
object[] array = new object[100];
then would:
array = null;
kill it? And do I have to point array to anything?
If by "killing it" you mean that C# disposes of the object, then the answer is not neccessarily. When C# executes the code object[] array = new object[100]; is reserves the memory space needed for the array. However, when you call array = null; it does exactly as you ask, set the array equal to null, nothing, nada, zero. In other words, the object array that you defined is not referencing anything. But, if you are still using the variable array after setting it null, C#'s garbage clean-up crew will not dispose of the variable before you stop using it. If you would have done so in Java, then the array variable would probably have been disposed when you set it to null and if you would have used the variable after that, then you would have gotten an exception.
Just my view of things.... ;)
Typhoon
04-20-2005, 02:57 PM
My C# have been rusty, I've learned it when it was released but never got back due to change of platform. C# has a garbage collection process in the background much like that of java. It removes/destroys objects that are not referenced by any variable when the garbage collector is called.
** Just an extra
Note however that if you used the same object like this way,
object[] array = new object[100];
object[] array2 = array;
array=null;
would not destroy the object until array2 gets/is set to another object
nice
vBulletin® v3.8.9, Copyright ©2000-2019, vBulletin Solutions, Inc.