tcn*_*lan 9 c# vb.net using using-directives
在VB.Net中你可以做如下的事情,没有任何问题...只是忽略这是一个非常无用的类的事实:-)
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
Run Code Online (Sandbox Code Playgroud)
但是如果你在C#中做同样的事情......
using System;
public class Class1
{
public static Collections.Generic.List ArrayToList(string[] _array)
{
return new Collections.Generic.List(_array);
}
}
Run Code Online (Sandbox Code Playgroud)
您将在"Collections.Generic.List"返回的行上收到错误,说"找不到类型或命名空间名称'Collections'(您是否缺少using指令或程序集引用?)"
我知道你必须有一个使用System.Collections.Generic的using指令才能使用List,但我不知道为什么.我也不明白为什么我在函数声明中没有得到相同的错误,只是在return语句中.
我希望有人可以解释这一点,甚至可以将我推荐给一个解释它的technet页面.我四处寻找,但找不到任何解释这个概念的东西.
编辑:请注意,问题实际上是关于子命名空间的引用,例如在示例中能够引用系统中的集合.
这是因为VB.Net支持部分命名空间 ; C#没有.
使用Visual Basic,默认情况下会导入System,并自动解析子命名空间.
阅读本文的更多内容.
| 归档时间: |
|
| 查看次数: |
3277 次 |
| 最近记录: |