so, I was recently searching for using a small library for FTP in C# ... I got through this question to this class....
I'm wondering a bit about the sense of the return
statement in all their void methods...
Here's for example their delete method:
/* 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;
}
My question is:
has the return;
any reason or effect?
From looking at the page every method ends with the pattern
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return /* Whatever the "default" return value would be on a error */;
Having a return
as the last statement in a void method does not do anything to the program, my only guess is it is a pattern the poster of the article likes to follow. He had it on his other methods that returned a string...
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return "";
so he likely just wanted to keep the same pattern on the methods that returned void.
No.
This can be used for early returns from the method, but this isn't the case here - return;
at the end of a method is entirely redundant.
There is a perceptible difference in the generated IL code, as long as you have optimizations disabled , though. A void method with no return
statement will simply contain a ret
instruction, while adding a return;
at the end will add a branch instruction - a jump to the ret
instruction.
无需编写无意义的解释,答案很简单:没有任何意义
It has no effect but I've seen it as well. I speculate (!) that some like to end their methods with return
to have a larger visual indication on the end of the block than the closing bracket. It might also save you a split second if you change the return type at a later stage (from void to something else).
There are some issues with your code:
ftpReques
t will not be closed (resource leak) ftpRequest
)? Exception
smells return
is useless (your question) The revisoned code could be something like that:
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());
}
}
A return
statement at the end of a void
method provides no additional effect. It can be removed without changing the semantics of the method.
One may use this return
to simplify textual searching for the place from which the function returns, but it has no effect to the compiler.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.