Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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# 使用嵌套名称空间_C#_.net_Namespaces - Fatal编程技术网

C# 使用嵌套名称空间

C# 使用嵌套名称空间,c#,.net,namespaces,C#,.net,Namespaces,给定以下命名空间结构: namespace A { public static class MyClass { public static int MyInt; } } namespace A.A1 { public static class MyClass { public static int MyInt; } } namespace B { namespace B1 { public static class MyClass { public static int MyInt; } }

给定以下命名空间结构:

namespace A { public static class MyClass { public static int MyInt; } }
namespace A.A1 { public static class MyClass { public static int MyInt; } }

namespace B { namespace B1 { public static class MyClass { public static int MyInt; } } }
为什么我会有以下行为

namespace C {
    using A;
    using B;

    public class SomeClass {
        public void foo() {
            // Valid, but renders 'using' directives obsolete
            A.A1.MyClass.MyInt = 5;
            B.B1.MyClass.MyInt = 5;

            // Error: The type or namespace A1/B1 could not be found.
            A1.MyClass.MyInt = 5;
            B1.MyClass.MyInt = 5;
        }
    }
}

namespace D {
    using A.A1;
    using B.B1;

    public class SomeClass {
        public void bar() {
        // Valid, but renders 'using' directives obsolete
        A.A1.MyClass.MyInt = 5;
        B.B1.MyClass.MyInt = 5;

        // Error: MyClass is ambiguous (of course)
        MyClass.MyInt = 5;

        // Error: The type or namespace A1/B1 could not be found.
        A1.MyClass.MyInt = 5;
        }
    }
}
我相信在名称空间中使用句点与嵌套它具有相同的效果(即
名称空间a.A1{}
=
名称空间a{namespace A1{}}
),并且
using
指令允许我在将来的使用中省略该部分。不是这样吗?

从页面:

创建一个using指令来使用命名空间中的类型,而不必指定命名空间using指令不允许您访问嵌套在指定名称空间中的任何名称空间。

你不能做你想做的事

举一个简单的例子:

using System;

public class Foo
{
    public void Bar()
    {
        // Compilation error!
        // You need new System.Collections.Generic.List<int>();
        new Collections.Generic.List<int>();
    }
}
使用系统;
公开课Foo
{
公共空白栏()
{
//编译错误!
//您需要新的System.Collections.Generic.List();
new Collections.Generic.List();
}
}

如果创建带有句点的命名空间,则命名空间名称将包含句点。但是,您可以使用句点访问嵌套的命名空间。例如
名称空间A.A1
将命名为A.A1,但

   namespace A
    {
        namespace A1 { }
    }

您可以使用A.A1通过
访问它,因为您的所有3个类都具有相同的名称
MyInt
,您必须明确指出您所引用的名称空间中的类

这就是为什么这两个例子都会导致错误

// Error: MyClass is ambiguous
MyClass.MyInt = 5;
// Error: The type or namespace A1/B1 could not be found.
A1.MyClass.MyInt = 5;
如本例所示明确定义:

A.A1.MyClass.MyInt = 5;
B.B1.MyClass.MyInt = 5;
在这种情况下是完全正常的,使用部分是不必要的

虽然有时可能有同名的类,但这是非常罕见的,而且在相同的上下文中使用这两个类更为罕见


问题是你想达到什么目的?

你对行为的哪一部分感到惊讶?我并不奇怪
A1.MyClass.MyInt
不起作用-名称空间不是这样工作的。感觉就像你同时在问几个方面的问题(A1.MyClass.MyInt是否应该工作,以及A和B之间的区别)。不,不是。。。如果您使用系统执行
,您可以清楚地看到它
然后
新建集合.Generic.List()
A1.MyClass.MyInt
如果您已经在
A
名称空间中,则可以使用。@Liam是的,我知道为什么不明确:P