C# lists... again

I just started learning C# and Unity and I’m having trouble with lists and i’ve searched the forums for similar problems and their solutions. Most suggest adding “using System.Collections.Generic;” fixes the problem.

In the code bellow “List list = new List();” works fine, but all the other lists return “The type or namespace name list1’ could not be found.” errors.

Does anyone know what i’m doing wrong? I’m using Unity 4.3. Any help would be great, thanks!

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LearningScript : MonoBehaviour {


	// Use this for initialization
	void Start () {

		List<int> list = new List<int>();
		list.Add(2);
		list.Add(3);
		list.Add(5);
		list.Add(7);

		Debug.Log("This list has " + list.Count + " numbers");
		Debug.Log("The list index 0 is " + list[0]);
		Debug.Log("The list index 1 is " + list[1]);
		Debug.Log("The list index 2 is " + list[2]);
		Debug.Log("The list index 3 is " + list[3]);

		list<int> myInt = new list<int>();
		list<char> myChar = new list<char>();
		list<float> myFloat = new list<float>();
		list<bool> myBool = new list<bool>();

		// list<string> cars = new list<string>();
		// cars.Add("Kit");
		// cars.Add("Ford");
		// cars.Add("Fiat");
		// cars.Add("Porche");

		// Debug.Log("This list has " + cars.Count + " cars");
		// Debug.Log("The car at index 0 is " + cars[0]);
		// Debug.Log("The car at index 0 is " + cars[1]);
		// Debug.Log("The car at index 0 is " + cars[2]);
		// Debug.Log("The car at index 0 is " + cars[3]);

	}

	// Update is called once per frame
	void Update () {
		
	}
}

Case is important! Is it List (with capital L) not list (which in your scope is a variable).

Usually classes are named with capital first letter (e.g. Integer, List, Vector). You can see that your “list” is not even coloured, while the “List” in line 11 is purple. This is one indication that the class was found.

Check here more about…C# List

Walsh