I don't like methods have several return lines. So I created a return value with string result - and in every condition I write result = something...
But when I write "try-catch" mechanism, I have to set public string result. Because, if I return a result in try, compiler will launch error, and says not all codes have return value. If I write result = string.Empty to end of the method, resharper says, it's not reachable code. So, here an example, and here is my question;
"What is the perfect way to write "return" in a method ?"
public static string PingThatAddress(string hostAddress)
{
try
{
Ping ping = new Ping();
PingReply pingreply = ping.Send(hostAddress);
string result;
if (pingreply != null && pingreply.Status.ToString() != "TimedOut")
{
result = "Address: " + pingreply.Address + "\r"
+ "Roundtrip Time: " + pingreply.RoundtripTime + "\r"
+ "TTL (Time To Live): " + pingreply.Options.Ttl + "\r"
+ "Buffer Size: " + pingreply.Buffer.Length + "\r";
}
else
{
result = string.Empty;
}
return result;
}
catch (Exception pingError)
{
Debug.Fail(pingError.Message + " " + pingError);
}
//compiler error: THERE IS NO RETURN VALUE here?
}