【问题标题】:VB.NET Function with no return value, converted to C# gets return errors?没有返回值的 VB.NET 函数,转换为 C# 得到返回错误?
【发布时间】:2017-01-19 10:15:59
【问题描述】:

转换一些 VB.NET 代码。一些静态函数对一些通过引用传递的参数做了一些工作,但不返回任何东西。 VB.NET 函数中到底发生了什么,它们可以在没有返回值的情况下存在并且不会出现任何调试错误?布尔值会发生什么?

Overloads Shared Function ExampleMethod(ByRef buffer1() as Byte, ByRef buffer2() as Byte) As Boolean
'do stuff here, no return types
End Function

Overloads Shared Function ExampleMethod(ByRef buffer1() as Byte, ByRef buffer2 as Byte) As Boolean
'do stuff here, no return types
End Function

【问题讨论】:

标签: c# .net vb.net translate


【解决方案1】:

https://msdn.microsoft.com/en-us/library/sect4ck6.aspx

在 VB.Net 中,您可以或者使用Return 语句返回一个值,或者为函数名称分配一个值,例如:

    ExampleMethod = true
    Exit Function
End Function

接着说:

如果您使用退出函数而不为 name 分配值,则该过程将返回在 returntype 中指定的数据类型的默认值。如果未指定 returntype,则过程返回 Nothing,这是 Object 的默认值。

C# 更严格一点!

【讨论】:

    猜你喜欢
    • 2015-12-28
    • 2016-04-15
    • 2019-11-28
    • 2016-03-26
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 2020-12-27
    相关资源
    最近更新 更多