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

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

问题描述

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.)

这篇关于是否有等效于 C# 输出参数的 VB.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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