You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 14:27:04