- Home /
Reset Generic List To Empty(C#, Urgent!)
I need to reset a generic list(You know, from the System.Collections.Generic namespace) to a new list, bassicaly like its never been touched, entirely new. This is simply because when i choose a random gameobject from the list, it is sometimes a null gameobject, thus meaning that the rest of the script that does things with that gameobject does nothing. Thanks!
I really need an answer quick, so please if you have any idea tell me!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Example : $$anonymous$$onoBehaviour
{
List<string> testList = new List<string>();
void Start()
{
string[] temp = new string[]{"1", "2", "3", "4"};
testList.AddRange(temp);
print (testList.Count);
}
//TEST
void Update()
{
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.C) && testList.Count > 0)
{
testList.Clear();
print (testList.Count);
}
}
}
Answer by Spinnernicholas · Dec 05, 2013 at 08:28 PM
ListName.Clear();
//or
ListName = new List<T>();
I happened to answer a similar question recently - But @fafase re$$anonymous$$ded me of something - is that it's important to watch out not just to clear the list if you had a list of "Objects" (Components, GameObjects, etc) - basically anything that could be instantiated in the Unity scene - if so, then you have to Destroy
them as well, not just clearing the list that references them (ie a list of Enemies, Cubes, etc). If you don't do that and just clear the list, you'll end up with a memory leak if there's nobody else beside the list that references them.
You will not create a memory leak by doing what you said. One does not need to worry about memory leaks because you dont allocate and release memory in average Unity program$$anonymous$$g.
If there is an object that is only referenced in a list and you delete that reference by list.clear()
or something like list = new List<T>()
then the garbace collection will take care of unreferenced objects. To learn more about garbage collection go here.
Answer by Skuxxnosch · Mar 26, 2020 at 02:10 PM
Be aware that list.clear();
and list = new List<T>();
are not the same.
When you are asking for a "new list, bassicaly like its never been touched, entirely new" , then you should use list = new List<T>();
since it should just reference a newly allocated List with list.count
and list.capacity
being 0
. The now referenceless list is going to be garbage collected if not referenced somewhere else.
When you use list.clear()
, it will remove all references of the list entries resulting in a list.count == 0
. But the list.capacity
will remain the same as during the usage before (this is crucial for extremely long and memory intensive lists). The capacity is basically the space the list has for possible entries. If you want to have list.capacity == 0
, use list.TrimExcess();
. Useful links: List, Capacity
You have a good point.
I would say that in most case, people are going to use the list for the same task and therefore need a similar capacity. If that's the case, they should probably use list.Clear();
and save some cycles from the list expanding repeatedly.
But it would be a waste of memory to build a list of thousands of items, clear it and then only use it for a couple items. In that case, they should just create a new list.