VB.NET是否有C#out参数的等效项? [英] Is there a VB.NET equivalent of C# out parameters?

查看:342
本文介绍了VB.NET是否有C#out参数的等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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参数的等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆