Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么有一个;使用名称空间";然后";使用namespace.class“;_C#_Namespaces - Fatal编程技术网

C# 为什么有一个;使用名称空间";然后";使用namespace.class“;

C# 为什么有一个;使用名称空间";然后";使用namespace.class“;,c#,namespaces,C#,Namespaces,很抱歉标题不好,很难用一句话来解释 如果“using”语句允许我们使用名称空间中的所有代码,那么为什么需要使用名称空间然后使用Namespace.Class添加一个更限定的路径来执行相同的语句 我的意思是:假设我们有: using System; namespace MyProgram { class Program { static void Main() { Console.WriteLine(&

很抱歉标题不好,很难用一句话来解释

如果“using”语句允许我们使用名称空间中的所有代码,那么为什么需要
使用名称空间
然后使用Namespace.Class添加一个更限定的路径来执行相同的语句

我的意思是:假设我们有:

using System;
namespace MyProgram 
{
     class Program 
     {
          static void Main() 
          {
               Console.WriteLine("Hi there");
               Console.Readline();
          }
     }
}
显然,我们需要系统名称空间来使用Console类将内容打印到屏幕上。太好了

接下来,我们需要创建一个列表。为此,我们必须输入名称空间和类的路径,以访问允许我们创建列表的类:

using System;
using System.Collections.Generic;
namespace MyProgram 
{
     class Program 
     {
          static void Main() 
          {
               Console.WriteLine("Hi there");
               var l = new List<int>(){1,2,3};
               Console.Readline();
          }
     }
}
使用系统;
使用System.Collections.Generic;
名称空间MyProgram
{
班级计划
{
静态void Main()
{
Console.WriteLine(“你好”);
var l=新列表(){1,2,3};
Console.Readline();
}
}
}

为什么我们必须使用
语句添加新的?如果
使用系统
允许我们访问系统命名空间中的所有代码,并且System.Collections.Generic位于所述系统命名空间中,那么
不会使用系统语句允许我们访问它,使用System.Collections.Generic创建
冗余?

在这种情况下,
控制台是在
系统
命名空间内定义的静态类型。
List
类在
System.Collections.Generic
命名空间中定义。您只能使用直接在正在使用的名称空间中定义的类型,因此要使用
控制台
,您需要
系统
名称空间,但子名称空间中不包括其他类型。

Re标题;这不是一个类,而是另一个名称空间。重新查询;使用不会分层导入。我认为职业训练局的问题“不可复制”,因为它基于一个错误的假设
List
的全名是
System.Collections.Generic.List
。您可以编写
var l=new
System.Collections.Generic.List(){…}
,也可以使用System.Collections.Generic;`然后
var l=newlist(){…}
。A
使用
只在该名称空间中引入类型,而不在名称仅以
开头的任何名称空间中引入类型