.net:为什么在命名空间中使用int是不合法的?

odi*_*seh 0 .net c# types namespaces

为什么命名空间中有"int"是不合法的?"int"是一种类型.对?就像"上课"一样.我知道它们之间存在差异,但有两种数据类型.

namespace Ikco.Crp.UI.Common
{
    public int i;
    ....
    ....
}
Run Code Online (Sandbox Code Playgroud)

微软的想法是什么?

Mar*_*wis 12

定义类和变量之间存在差异.将类放入命名空间时,可以定义新类型.在您的声明中,您没有定义新类型.而是使用现有类型(int)来定义变量.使用类的等价物将是类似的

namespace Foo{
  class Bar{}   // type definition
  Bar the_bar;  // variable definition
}
Run Code Online (Sandbox Code Playgroud)

第二个定义也是不允许的,因此类和int的行为确实相同:C#中没有全局变量.