为什么我不能在对象初始化期间使用部分限定的命名空间? [英] Why can't i use partly qualified namespaces during object initialization?

查看:148
本文介绍了为什么我不能在对象初始化期间使用部分限定的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑这是一个问题,以前被问过很多次,但我还没有找到一个。

I suspect this is a question which has been asked many times before but i haven't found one.

我通常使用完全限定的命名空间,如果我不经常在文件中使用该类型或者使用namaspacename添加 在文件的顶部能够写新的ClassName()

I normally use fully qualified namespaces if i don't use that type often in the file or i add using namaspacename at the top of the file to be able to write new ClassName().

添加了完整命名空间的一部分?为什么编译器找不到类型并抛出一个错误?

But what if only a part of the full namespace was added ? Why can't the compiler find the type and throws an error?

考虑在嵌套命名空间下面的类:

Consider following class in a nested namespace:

namespace ns_1
{
    namespace ns_1_1
    {
        public class Foo { }
    }
}

所以如果我现在想初始化这个类的实例,以下方式:

So if i now want to initialize an instance of this class, it works in following ways:

using ns_1.ns_1_1;

public class Program
{
    public Program()
    {
        // works, fully qualified namespace:
        var foo = new ns_1.ns_1_1.Foo();
        // works, because of using ns_1.ns_1_1:
        foo = new Foo();
    }
}

但以下不起作用:

using ns_1;

public class Program
{
    public Program()
    {
        // doesn't work even if using ns_1 was added
        var no_foo = new ns_1_1.Foo();
    }
}

它会引发编译器错误:


无法找到类型或命名空间名称ns_1_1(您是
缺少using指令还是程序集引用?)

The type or namespace name 'ns_1_1' could not be found (are you missing a using directive or an assembly reference?)

我假设因为 ns_1_1 被视为包含另一个类 Foo 而不是命名空间,这是正确的吗?

I assume because ns_1_1 is treated like a class which contains another class Foo instead of a namespace, is this correct?

我没有找到语言规范,这是记录在哪里?为什么编译器不够聪明,无法检查是否有 命名空间(-part)?

I haven't found the language specification, where is this documented? Why is the compiler not smart enough to check if there's a class or namespace(-part)?

这里是另一个 - 不太抽象 - 我的意思的例子:

Here's another - less abstract - example of what i mean:

using System.Data;

public class Program
{
    public Program()
    {
        using (var con = new SqlClient.SqlConnection("...")) // doesn't work
        {
            //... 
        }
    }
}

编辑:现在我知道为什么这看起来很奇怪。它在VB.NET中没有问题:

Edit: now i know why this seems very strange to me. It works without a problem in VB.NET:

Imports System.Data

Public Class Program
    Public Sub New()
        Using con = New SqlClient.SqlConnection("...") ' no problem

        End Using
    End Sub
End Class


推荐答案

文档说:


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

因此使用只包括在指定的命名空间中定义的类型(而不是命名空间)。为了访问嵌套命名空间的类型,您需要使用指令明确指定它,如第一个例子所示。

So the using only includes the types (not the namespaces) that are defined in the specified namespace. In order to access types of nested namespace you need to specify it explicitly with a using directive as you did in your first example.

这篇关于为什么我不能在对象初始化期间使用部分限定的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆