如何在VS Code中正确运行C#控制台应用并解决List<>找不到报错
问题根因
List<T> 是C#提供的泛型集合类,归属于 System.Collections.Generic 命名空间,你当前代码仅引入了 System 命名空间,未对该命名空间做引用声明,也没有在使用时写类的完整限定名,因此编译器无法识别 List<> 类型,触发编译报错。
修复方案
两种方案二选一即可:
- 方案一:在代码头部添加命名空间引用
using System; // 新增以下引用语句 using System.Collections.Generic; class Program { static void Main(string[] args) { List<int> list = new List<int>(); } }
- 方案二:使用时写全类的限定名,无需额外加引用
using System; class Program { static void Main(string[] args) { System.Collections.Generic.List<int> list = new System.Collections.Generic.List<int>(); } }
注意:执行运行命令时不要携带注释内容,正确的运行指令为
dotnet run。
修复完成后重新执行运行命令即可正常编译运行。
内容的提问来源于stack exchange,提问作者MOHAMED ABUELATTA
相关产品推荐
相关产品推荐

