C# The type or namespace name `List' could not be found. But I'm importing System.Collections.Generic;

CalebB picture CalebB · Jul 30, 2015 · Viewed 56.9k times · Source

I'm having an error

The type or namespace name `List' could not be found. Are you missing a using directive or an assembly reference?

Sample code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class city1 : MonoBehaviour 
{  
   public static List<string> items = new List ();
   public static List<double> itemsprice = new List();
   public static List<double> qu = new List();
}

I'm using mono if it matters.

Answer

Chris Sinclair picture Chris Sinclair · Jul 30, 2015

The issue comes from your instantiation of new List(). These also need the generic component:

public static List<string> items = new List<string>();
public static List<double> itemsprice = new List<double>();
public static List<double> qu = new List<double>();

That is, there is no type List but there is a generic type List<T>.

More information and examples of instantiating the generic List<T> can be found in the MSDN documentation.