C# void方法中的return语句

C# void方法中的return语句,c#,return,void,C#,Return,Void,所以,我最近正在寻找使用一个小型库来实现C#中的FTP。。。我把问题讲完了 我想知道return语句在所有void方法中的意义 以下是他们的删除方法示例: /* Delete File */ public void delete(string deleteFile) { try { /* Create an FTP Request */ ftpRequest = (FtpWebRequest)WebRequest.Create(host

所以,我最近正在寻找使用一个小型库来实现C#中的FTP。。。我把问题讲完了

我想知道
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是完全冗余的


不过,只要禁用了优化,生成的IL代码就会有明显的差异。没有
return
语句的void方法将只包含
ret
指令,同时添加
return将添加一条分支指令—跳转到
ret
指令。

无需编写无意义的解释,答案很简单:它没有任何意义在
void
方法结尾处的return
语句不会提供额外的效果。可以在不改变方法语义的情况下删除它


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

从查看页面开始,每个方法都以模式结束

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的方法上保持相同的模式。

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

您的代码存在一些问题:

  • 如果抛出异常,
    ftpReques
    t将不会关闭(资源泄漏)
  • 是否确实要重新创建类字段(
    ftpRequest
  • 捕捉
    异常
    气味
  • 最后一个
    返回值
    无效(您的问题)
  • 修改后的代码可能是这样的:

    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());
      }
    }
    

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