【发布时间】:2010-11-21 20:43:33
【问题描述】:
在 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
但是,如果你在 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 指令或程序集引用?)”
我知道您实际上必须有一个指向 System.Collections.Generic 的 using 指令才能使用 List,但我不知道 为什么。我也不明白为什么我在函数声明中没有得到同样的错误,而只是在 return 语句中。
我希望有人可以解释这一点,或者甚至将我引至解释它的技术网页面。我四处搜索,但找不到任何解释这个概念的东西。
编辑:请注意,问题实际上是关于子命名空间的引用,例如在示例中能够引用系统内的集合。
【问题讨论】:
标签: c# vb.net using using-directives