Help understanding this generic list in JS.

I’m having touble finding info on generic lists for js online. The scripting reference has nothing on the and the tut video are for c# and the js tab under he tut video for js says that js is too different from the video, thus making a translation unusable. Somewhere else it said that js can’t use generic lists(might of been the scripting reference). However I finally found some code and understood what most of it does. I just need help understanding like 4 lines. Truth be told, I believe it’s a generic list but I’m not even sure.

#pragma strict
import System.Collections.Generic;

function Start ()

    var list : List.<String>;

    list = new List.<String>();

    //Capacity is the number of elements that the List<T> can store before resizing is required

    //Count is the number of elements that are actually in the List<T>.

    Debug.Log("list capacity is " +list.Capacity);

    Debug.Log("list count is " +list.Count);

    Debug.Log("Adding 4 entries");


    list.Add(" work");

    list.Add(" in");

    list.Add(" js");

    list.Add(" too.");

    Debug.Log("Note that now capacity is bigger than count"); 

    Debug.Log("list capacity is " +list.Capacity);

    Debug.Log("list count is " +list.Count);

    //js cannot have foreach but can use for in

    var sentence:String;

    for( var word:String in list)


        sentence +=word;



    Debug.Log("list.Contains(\"generics\") is " + list.Contains("generics"));

    Debug.Log("list.Insert(5, \" Unity js rocks !\");" +" inserts a new item at the end of the list");

    list.Insert(5, " Unity js rocks !");

    Debug.Log("Let's check it");

    sentence = "";

    for( var word:String in list)


       sentence +=word;


    //For more methods of List check out

    //Or google c sharp List

line#9: Why does he need to create a new list when he already created it on the line above?

line#19: He says adding 4 entries but he adds 5, is it just a typo?

line#33: Exactly, capacity end up being 8 and count ends up being 5, but why? If they both started at 0, and then he added 5 things, why does capacity jump up to 8?

line#60: What does the 5 do? Does it add something after the 5 thing in the list which would be “too”, or does it add something that’s 5 entries long?

line#64: I commented this out and debuglog printed “Generics work in js too” twice. So I know what it does I just dont understand why.

Sorry for all the questions. I used to do that before but now I usually stick to figuring out everything on my own. The problem is that the info on this is scarce so I could use the help.

##Line: 9

As stated before by @getyour411, the difference between line 9 and line 7 are that line 7 declares a variable (list of type List.), while line 9 creates a new instance of a list and assigns that instance to list. Because List is a reference type, one that cannot be passed by value, it needs an instance to work.

Ideally, because the type is obvious, those lines should be written as simply:

var list = List.<String>();

Or if you love declaring types:

var list:List.<String> = List.<String>();

For more information on types, references, objects, structures etc. Do some research on OO concepts.

##Line: 19

I think we’ve established thats a typo

##Line: 33

Remember that List is a .NET type, so it’s documentation is intuitively on msdn:

Capacity is the number of elements that the List can store before resizing is required, whereas Count is the number of elements that are actually in the List.
Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.
If the capacity is significantly larger than the count and you want to reduce the memory used by the List, you can decrease capacity by calling the TrimExcess method or by setting the Capacity property explicitly. When the value of Capacity is set explicitly, the internal array is also reallocated to accommodate the specified capacity, and all the elements are copied.

A good thing to note is that List uses a built-in array internally.

##Line: 60

Again, this can be seen in the .NET documentation:

Inserts an element into the List at the specified index.


  • index:int, The zero-based index at which item should be inserted.
  • item:T, The object to insert. The value can be null for reference types.

##Line: 64

By simply desk-checking (running through the program in our head) we can see that at line 64, sentence will be a construction made up of all the elements in list, concatenated into a single String. If we were to run the code following line 64, the next time sentence is logged, it will have two sentences in one String. As this is not the required behaviour, sentence is set back to an empty String.

To make it more obvious line 43 could also be written as:

var sentence = "";

Or again, with typing:

var sentence:String = "";


Just as a final note, if you’re ending for loops like this:

statement) {

Then they should also start with the same spacing:

for (statement


for( statement

Remember: the ( is wrapping the statement, not trailing the for.