当我们已经使用了“using System”时,为什么还要使用“using System.Collections.Generic”之类的语句?

Jai*_*rma -3 c#

C# 代码

我是一名新的 c# 学习者,这就是为什么我有一些最愚蠢的问题!但我想知道我们是否已经在应用程序中使用了“using System”语句,那么为什么我们需要使用“using System.Data”或图像中显示的任何其他语句。主命名空间不是已经导入了吗,为什么还需要为子命名空间添加using语句呢?

Swe*_*per 5

因为语言规范是这样说的:

C# 语言规范第 9.4.2 节使用命名空间指令:

using-namespace-directive 导入给定命名空间中包含的类型,但具体不导入嵌套命名空间

原因可能是,如果导入每个嵌套命名空间,则会出现太多名称冲突。这降低了使用指令的好处using

using如果指令导入嵌套命名空间,我可以想到以下一些名称冲突:

  • System.Drawing.PathSystem.IO.Path
  • System.Windows.Forms.ButtonSystem.Web.UI.WebControls.Button
  • System.Timers.TimerSystem.Threading.TimerSystem.Windows.Forms.Timer