VB.NET是否直接等效于C# out 函数参数,其中传递给函数的变量不需要初始化?
Does VB.NET have a direct equivalent to C# out function parameters, where the variable passed into a function does not need to be initialised?
推荐答案不,在VB中没有 out 关键字。
No, there is no equivalent of the out keyword in VB.
但是,VB会自动初始化方法中的所有局部变量,因此您可以使用 ByRef 而不需要先显式初始化变量。
However, VB does automatically initialise all local variables in a method, so you can use ByRef without needing to explicitly initialise the variable first.
示例:
Sub Main() Dim y As Integer Test(y) End Sub Sub Test(ByRef x As Integer) x = 42 End Sub(如果您检查框架中的代码(例如 Double.TryParse ),您可能会看到< OutAttribute> 已添加到参数中,但这仅在将调用编组以进行COM互操作或平台调用时有所不同。)
(If you examine code in the framework (for example Double.TryParse), you may see the <OutAttribute> added to parameters, but that only makes a difference when the call is marshalled for COM interop or platform invoke.)
更多推荐
VB.NET是否有C#out参数的等效项?
发布评论