C#中的命名空间范围

Exp*_*ser 3 c# scope namespaces

命名空间的名称是否也定义了类中的范围?

  1. 例如,我有默认情况下由解决方案命名的命名空间.让我们说在Program.cs名称空间是"MySolution".
  2. 在我创建的下一个文件界面中,命名空间的名称是"Interfaces"

当我回到课堂上,为了实现这个界面,它不在范围内(我在intellisense中找不到它).因此,如果类有一个命名空间,并且接口有第二个命名空间,那么我不能在我的类中使用它.我认为命名空间仅用于组织类(如姓氏),但我想它也定义了范围.

Bra*_*ell 5

"namespace关键字用于声明包含一组相关对象的范围.您可以使用命名空间来组织代码元素并创建全局唯一类型." https://msdn.microsoft.com/en-us/library/z2kcy19k.aspx

要从另一个命名空间使用类,接口,结构,枚举,委托,您需要使用该类型的完全限定名称,或者using Interface;在类的顶部声明对该命名空间的引用.IE:

using Interfaces;  //can reference namespaces here

namespace MySolution
{
    public class Program 
    {
        //or you can use the fully qualified name of the type
        public Program(Interfaces.MyInterface example) 
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)