【问题标题】:return statement in void methodsvoid 方法中的 return 语句
【发布时间】:2015-09-21 12:19:38
【问题描述】:

所以,我最近正在寻找在 C# 中使用 FTP 的小型库...我通过this 问题到this 类...。

我有点想知道 return 声明在他们所有的 void 方法中的意义......

下面是他们的删除方法:

     /* Delete File */
public void delete(string deleteFile)
{
    try
    {
        /* Create an FTP Request */
        ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
        /* Log in to the FTP Server with the User Name and Password Provided */
        ftpRequest.Credentials = new NetworkCredential(user, pass);
        /* When in doubt, use these options */
        ftpRequest.UseBinary = true;
        ftpRequest.UsePassive = true;
        ftpRequest.KeepAlive = true;
        /* Specify the Type of FTP Request */
        ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
        /* Establish Return Communication with the FTP Server */
        ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        /* Resource Cleanup */
        ftpResponse.Close();
        ftpRequest = null;
    }
    catch (Exception ex) { Console.WriteLine(ex.ToString()); }
    return;
}

我的问题是:

return; 有任何理由或影响吗?

【问题讨论】:

  • return 只是在该行结束函数。如果函数有一个实际的类型返回,那么它会返回一个值(如果指定了一个值)。
  • 一般来说,这段代码的风格有问题,在每个方法中都使用try/catch,并捕获所有可能的异常。我不建议将其用作编码风格的示例。

标签: c# return void


【解决方案1】:

没有。

这可以用于方法的早期返回,但这里不是这种情况 - 方法末尾的return; 完全是多余的。

生成的 IL 代码存在明显差异,只要您禁用了优化。没有 return 语句的 void 方法将仅包含 ret 指令,而在末尾添加 return; 将添加分支指令 - 跳转到 ret 指令。

【讨论】:

    【解决方案2】:

    不用写无意义的解释,答案很简单:没有任何意义

    【讨论】:

      【解决方案3】:

      void 方法末尾的 return 语句不会提供额外的效果。可以在不改变方法语义的情况下将其移除。

      可以使用这个return 来简化对函数返回位置的文本搜索,但它对编译器没有影响。

      【讨论】:

        【解决方案4】:

        通过查看页面,每个方法都以模式结束

        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return /* Whatever the "default" return value would be on a error */;
        

        return 作为 void 方法中的最后一个语句对程序没有任何作用,我唯一的猜测是文章的发布者喜欢遵循的模式。他在返回字符串的其他方法中使用了它...

        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return "";
        

        所以他可能只是想在返回 void 的方法上保持相同的模式。

        【讨论】:

          【解决方案5】:

          它没有效果,但我也看到了。我推测(!)有些人喜欢用return 结束他们的方法,以便在块的末尾有比右括号更大的视觉指示。如果您在稍后阶段更改返回类型(从 void 更改为其他内容),它也可能会为您节省一秒钟的时间。

          【讨论】:

            【解决方案6】:

            您的代码存在一些问题

            1. 如果抛出异常,ftpRequest 将不会关闭(资源泄漏)
            2. 您真的要重新创建一个类字段 (ftpRequest) 吗?
            3. 捕捉Exception 气味
            4. 最后一个return 没用(你的问题)

            修改后的代码可能是这样的:

            public void delete(string deleteFile) {
              try {
                // using over IDisposable is 
                using (var ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile)) {
                  ftpRequest.Credentials = new NetworkCredential(user, pass);
                  // When in doubt, use these options 
                  ftpRequest.UseBinary = true;
                  ftpRequest.UsePassive = true;
                  ftpRequest.KeepAlive = true;
                  // Specify the Type of FTP Request 
                  ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                  // Establish Return Communication with the FTP Server 
                  ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                }
              }
              catch (WebException ex) {
                Console.WriteLine(ex.ToString());
              }
            }
            

            【讨论】:

              猜你喜欢
              • 2013-05-23
              • 2017-12-04
              • 2012-07-06
              • 2014-08-31
              • 1970-01-01
              • 2023-03-19
              • 2023-03-27
              相关资源
              最近更新 更多