本文介绍了什么是写一个完美的方式"回归"在一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢的方法有几个回油管路。所以,我创建了字符串结果返回值 - 在我写的结果=什么......



但是,当我写的try-catch的机制,我有充分的条件设置的公共字符串结果的。因为,如果我尝试返回结果,编译器会启动错误,并说不是所有的代码都返回值。如果我写的结果=的String.Empty 的该方法的结束,ReSharper的说,这是不可达的代码。所以,在这里一个例子,这里是我的问题,?在方法



什么是写最完美的方式回报

 公共静态字符串PingThatAddress(字符串hostAddress)
{

{
坪坪=新平();
PingReply pingreply = ping.Send(hostAddress);

字符串结果;
如果(pingreply = NULL&放大器;&安培; pingreply.Status.ToString()=TIMEDOUT!)
{
结果=地址:+ pingreply.Address +\ R
+往返时间:+ pingreply.RoundtripTime +\r
+的TTL(存活时间):+ pingreply.Options.Ttl +\r
+缓冲大小:+ pingreply.Buffer.Length +\r
}
,否则
{
结果=的String.Empty;
}

返回结果;
}
赶上(例外pingError)
{
Debug.Fail(pingError.Message ++ pingError);
}
//编译器错误:没有返回值吗?
}


解决方案

您可以像下面这样做而不是:

 公共静态字符串PingThatAddress(字符串hostAddress)
{
字符串结果=的String.Empty;

{
坪坪=新平();
PingReply pingreply = ping.Send(hostAddress);

如果(pingreply = NULL&放大器;&安培; pingreply.Status.ToString()=TIMEDOUT!)
{
结果=地址:+ pingreply.Address +\r
+往返时间:+ pingreply.RoundtripTime +\r
+的TTL(存活时间):+ pingreply.Options.Ttl +\ R
+缓冲大小:+ pingreply.Buffer.Length +\r
}

}
赶上(例外pingError)
{
Debug.Fail(pingError.Message ++ pingError);
}
返回结果;
}



然后,只需确保结果设置为一些有意义的异常的情况下



如果你想坚持什么ReSharper的警告你一下,做这种方式:

 公共静态字符串PingThatAddress(字符串hostAddress)
{

{
坪坪=新平();
PingReply pingreply = ping.Send(hostAddress);

字符串结果=的String.Empty;
如果(pingreply = NULL&放大器;&安培; pingreply.Status.ToString()=TIMEDOUT!)
{
结果=地址:+ pingreply.Address +\ R
+往返时间:+ pingreply.RoundtripTime +\r
+的TTL(存活时间):+ pingreply.Options.Ttl +\r
+缓冲大小:+ pingreply.Buffer.Length +\r
}
返回结果;

}
赶上(例外pingError)
{
Debug.Fail(pingError.Message ++ pingError);
}
返回的String.Empty;
}

您不能有东西两种方式在这里:你的人为标准不具有多重收益语句可能是什么原因造成你有ReSharper的麻烦。


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?
    }
解决方案

You could do it like this instead:

public static string PingThatAddress(string hostAddress)
{
    string result = string.Empty;
    try
    {
        Ping ping = new Ping();
        PingReply pingreply = ping.Send(hostAddress);

        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";
        }

    }
    catch (Exception pingError)
    {
        Debug.Fail(pingError.Message + " " + pingError);
    }
    return result;
}

Then just make sure result is set to something that makes sense in the case of an exception.

If you are trying to stick with what Resharper is warning you about, do it this way:

public static string PingThatAddress(string hostAddress)
{
    try
    {
        Ping ping = new Ping();
        PingReply pingreply = ping.Send(hostAddress);

        string result = string.Empty;
        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";
        }
        return result;

    }
    catch (Exception pingError)
    {
        Debug.Fail(pingError.Message + " " + pingError);
    }
    return string.Empty;
}

You can not have things both ways here: Your artificial standard not to have multiple return statements is probably what is causing you to have trouble with Resharper.

这篇关于什么是写一个完美的方式"回归"在一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:14