c# - 设置一个函数等于某物

标签 c# vb.net

我正在将 VB.Net 程序转换为 C#。写VB.Net代码的程序员离开了,所以我不能向他求助,而且我对VB.net的了解也很少。在他的程序中,他有以下内容:

public DataSet getData(string SQL)
{

  'Some variable declarations
  Dim ds As New DataSet

  Try
    'Some code
    getData = ds 'This is the part of the code I am having trouble figuring out.

  'some more code
}

所以 getData 是函数的名称,他将其设置为自身内部的“ds”。所以我的问题是,为什么要将函数设置为等于其内部的某个东西? “getData = ds”的正确 C# 版本是什么?

最佳答案

这是一些 VB 语法,表示函数的返回值为 dsreturn ds; 在 C# 中是等价的

来自 this page , 你可以看到这是 VB 中的有效返回方法(强调我的)

Returning from a Function

When the Function procedure returns to the calling code, execution continues with the statement that follows the statement that called the procedure.

To return a value from a function, you can either assign the value to the function name or include it in a Return statement.

但是我发现这使得代码很难阅读,所以我倾向于远离它而更喜欢返回

关于c# - 设置一个函数等于某物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26469027/

相关文章:

javascript - 无法在javascript方法中传递ajax post

C# 无法将 T 转换为 T

vb.net - 为什么 DateTimePicker 不会用 tab 键触发 keyDown 和 KeyPress 事件?

c# - 在 MVC 中使用 Web API DelegatingHandler

c# - 在 LINQ 查询中使用来自反射的值

c# - 尝试使用代码将一行插入数据库,然后在单击按钮后显示该行(使用 CommandText)

vb.net - DataContract WCF 中的命名空间

mysql - DataGridView选择多行,检查每一行

c# - 在 Asp.net 中缓存(slidingExpiration 和 absoluteExpiration)

C# 到 VB .NET IntPtr 转换