在 vb.net 中有一种奇怪的方法,您可以使用函数名称作为结果变量..
例子:
Function Foo(ByVal bar As Integer) As List(Of Integer)
Foo = New List(Of Integer)
Foo.Add(bar + 1)
End Function
据我所知,在 C# 中你必须:
List<int> foo(int bar)
{
var result = new List<int>();
result.Add(bar + 1);
return result;
}
我不确定这是设计使然还是我不知道正确的方法.. 请赐教!
提前致谢, 埃坦。
请您参考如下方法:
C# 不支持。从历史上看,VB 支持这一点的唯一原因是以前的(.NET 之前的)版本专门使用函数名称分配——即没有 Return声明。1
.NET 开发人员普遍认为您应该使用 Return …方法甚至在 VB 中而不是使用 FunctionName = …和 Exit Function .
1 嗯,实际上他们做到了,但它做了其他事情。
