C#与VB.Net中的命名空间引用 [英] Namespace references in C# vs. VB.Net
问题描述
在VB.Net中,您可以执行以下操作,而不会出现任何问题...只需忽略以下事实:这是一个非常无用的类:-)
In VB.Net you can do something like the following without any issues... just ignore the fact that this is a pretty useless class :-)
Imports System
Public Class Class1
Public Shared Function ArrayToList(ByVal _array() As String) As Collections.Generic.List(Of String)
Return New Collections.Generic.List(Of String)(_array)
End Function
End Class
但是,如果您在C#中执行相同的操作...
However if you do the same thing in C#...
using System;
public class Class1
{
public static Collections.Generic.List ArrayToList(string[] _array)
{
return new Collections.Generic.List(_array);
}
}
您将在返回"Collections.Generic.List"的行上收到一条错误消息,提示找不到类型或名称空间名称'Collections'(您是否缺少using指令或程序集引用?)"
You will get an error on the line with the return on "Collections.Generic.List" saying "The type or namespace name 'Collections' could not be found (are you missing a using directive or an assembly reference?)"
我知道您实际上必须对System.Collections.Generic使用using指令才能使用List,但我不知道为什么 .我也不明白为什么在函数声明中没有得到相同的错误,而在return语句中却没有得到相同的错误.
I know that you have to actually have a using directive to System.Collections.Generic to use List but I don't know why. I also don't understand why I don't get the same error in the function declaration, but only in the return statement.
我希望有人可以对此进行解释,甚至希望我进入解释它的technet页面.我四处搜寻,但找不到任何能解释这个概念的东西.
I was hoping someone can explain this or even refer me to a technet page that explains it. I have searched around, but can't find anything that explains this concept.
仅需注意,问题实际上是关于子命名空间的引用,例如在示例中能够引用System中的Collections.
Just to note, the question is really about the referencing of a sub-namespace such as in the example being able to reference Collections within System.
推荐答案
创建一个using指令以使用
无需在名称空间中键入
指定名称空间.一个使用
指令无法让您访问
嵌套在
您指定的名称空间.
Create a using directive to use the
types in a namespace without having to
specify the namespace. A using
directive does not give you access to
any namespaces that are nested in the
namespace you specify. VB.NET,但是,支持一些与 VB.NET, however, supports somewhat closer behavior with 所作要素的范围
由Imports声明提供
取决于您的具体时间
使用Imports语句.为了
例如,如果只有一个名称空间
指定的,所有唯一命名的成员
该名称空间的成员,以及
该名称空间中的模块是
无条件提供.如果
命名空间和名称
该名称空间的元素是
指定,只有该成员
元素可不带
资格. The scope of the elements made
available by an Imports statement
depends on how specific you are when
using the Imports statement. For
example, if only a namespace is
specified, all uniquely named members
of that namespace, and members of
modules within that namespace, are
available without qualification. If
both a namespace and the name of an
element of that namespace are
specified, only the members of that
element are available without
qualification. 这篇关于C#与VB.Net中的命名空间引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!using
指令
Imports
语句更接近的行为:Imports
statement: