如何减少此代码中使用的变量?

时间:2016-09-12 19:46:50

标签: c#

我正在尝试减少下面代码中使用的变量数量 理想情况下,我希望在不创建LatestRipMe变量的情况下继续重用字符串变量LatestRipMeVersion string[] 我需要创建一个数组才能执行Skip(1)First()吗? 我能否更有效地达到LatestRipMe的最终价值?

private void RipMeGetLatestVersion_Process()
{
    //Get the .json file and save it in LatestRipMe string
    LatestRipMe = ClientRipMe.DownloadString("http://www.rarchives.com/ripme.json");

    //Create an array from the previously saved string, and skip the first line
    LatestRipMeVersion = LatestRipMe.Split(Environment.NewLine.ToCharArray()).Skip(1).ToArray();

    //Put the array back in a string, and select only the first line
    LatestRipMe = LatestRipMeVersion.First();

    //The characters which need to be removed
    char[] LatestRipMeTrim = { ' ', ' ', '"', 'l', 'a', 't', 'e', 's', 't', 'V', 'e', 'r', 's', 'i', 'o', 'n', '"', ' ', ':', ' ', '"', '"', ',' };

    //Trim the above declared characters
    LatestRipMe = LatestRipMe.Trim(LatestRipMeTrim);

    //Show the remaining string content in the TextBlock
    LatestRipMeVersionText.Text = "Latest RipMe version: " + LatestRipMe;
}

4 个答案:

答案 0 :(得分:1)

您可以替换

  //Create an array from the previously saved string, and skip the first line
  LatestRipMeVersion = LatestRipMe.Split(Environment.NewLine.ToCharArray()).Skip(1).ToArray();

  //Put the array back in a string, and select only the first line
  LatestRipMe = LatestRipMeVersion.First();

  LatestRipMe = LatestRipMe.Split(Environment.NewLine.ToCharArray())[1];

甚至

  LatestRipMe = LatestRipMe
           .Split(Environment.NewLine.ToCharArray())[1]
           .Trim(new char[] { ' ', 'a', 'b' });  // etc ...

观点:
风格明智,这是丑陋的,难以调试,并将抛出异常网站不返回您的期望(没有互联网连接等)。我并不真正理解为了减少变量的数量而更换清晰的,逐步记录的逐步代码的愿望,除非它是编程挑战。

答案 1 :(得分:0)

private void RipMeGetLatestVersion_Process()
{
    LatestRipMe = ClientRipMe.DownloadString("http://www.rarchives.com/ripme.json");

    LatestRipMeVersion = LatestRipMe.Split(Environment.NewLine.ToCharArray())[1];

    LatestRipMe = LatestRipMe.Trim("  \"latestVersion\" : \"\",".ToCharArray());

    LatestRipMeVersionText.Text = "Latest RipMe version: " + LatestRipMe;
}

答案 2 :(得分:0)

这应该会给你你想要的东西,但要注意这段代码有很多可能出错的地方。

private void RipMeGetLatestVersion_Process()
    {
        LatestRipMeVersion = ClientRipMe
            .DownloadString("http://www.rarchives.com/ripme.json")
            .Split(Environment.NewLine.ToCharArray())
            .Skip(1)
            .ToArray();

        LatestRipMe = LatestRipMeVersion.First()
            .Trim(' ', ' ', '"', 'l', 'a', 't', 'e', 's', 't', 'V', 'e', 
            'r', 's', 'i', 'o', 'n', '"', ' ', ':', ' ', '"', '"', ',');
        LatestRipMeVersionText.Text = "Latest RipMe version: " + LatestRipMe;
    }

答案 3 :(得分:0)

很多人已经告诉过你,这段代码有太多问题。您应该使用JSON解析器并遵循一些标准约定来命名和构造代码。我觉得你的代码仍然没有做你想做的事情。看看我下面的版本,你可以正确地做你想要的东西(从json获取属性latestVersion的值)。没有想象力,这是很好的代码,但比你拥有的要好得多

static void Caller() {
    LatestRipMeVersionText.Text = "Latest RipMe version: " + GetLatestVersion();
}

private string GetLatestVersion() {
    var ripmeJson = ClientRipMe.DownloadString("http://www.rarchives.com/ripme.json").      //Get the .json file and save it in LatestRipMe string
        Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)[1].     // convert to an array and pick the 2nd element
        Split(':')[1].Trim('"', ',', ' ');                                                  // fetch the value part

    return ripmeJson;
}