ArrayList in C#

Hi all,

I am having a strange behaviour with an ArrayList and maybe you could help me.

GestureTemplates.Templates.Add(GestureRecognizer.newTemplateArr);
ArrayList a = (ArrayList)GestureTemplates.Templates[GestureTemplates.Templates.Count - 1];
Debug.Log("1. Size of new template " + a.Count);
GestureRecognizer.newTemplateArr.Clear();
a = (ArrayList)GestureTemplates.Templates[GestureTemplates.Templates.Count - 1];
Debug.Log("2. Size of new template " + a.Count);

In the first Log it returns me 64 but after performing the Clear() it returns me 0. I thought the ArrayList.Add() function was making a copy of the array. Both GestureTemplates.Templates and GestureRecognizer.newTemplateArr are static. What I am missing?

Thanks in advance :slight_smile:

The line:

GestureTemplates.Templates.Add(GestureRecognizer.newTemplateArr);

adds a reference to the GestureRecognizer.newTemplateArr array list to the end of the GestureTemplates.Templates array list. The GestureRecognizer.newTemplateArr variable, the last index in the GestureTemplates.Templates array list and the a variable all reference the same data.

If you want to duplicate an array list, you need to do it explicitly - like this:

GestureTemplates.Templates.Add(new ArrayList (GestureRecognizer.newTemplateArr));