C# 在函数中检测到无法访问的代码

C# 在函数中检测到无法访问的代码,c#,winforms,visual-studio,C#,Winforms,Visual Studio,运行VS时,我收到无法访问的代码检测错误。我在以下两行收到错误: MoveListItemslistBox1、listBox2和ReplaceListItemslistBox1、listBox2 感谢您的帮助 代码如下: private string CreateNewEntry(string current) { var indexIn = current.LastIndexOf("Time In : "); // Get the last index of t

运行VS时,我收到无法访问的代码检测错误。我在以下两行收到错误:

MoveListItemslistBox1、listBox2和ReplaceListItemslistBox1、listBox2

感谢您的帮助

代码如下:

    private string CreateNewEntry(string current)
    {
        var indexIn = current.LastIndexOf("Time In : "); // Get the last index of the word "in"
        var indexOut = current.LastIndexOf("Time Out : "); // Get the last index of the word out

        if (indexOut > indexIn)
        {
            return current + "      "+"Time In : "; // if the last "out" comes after the last "in"
            ReplaceListBoxItems(listBox1,listBox2);
        }
        else
        {
            // If the last "in" comes after the last "out"
            return current + "      " +"Time Out : ";
            MoveListBoxItems(listBox1,listBox2);
        }
    }

    private void MoveListBoxItems(ListBox source, ListBox destination)
    {
        ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
        foreach (var item in sourceItems)
        {
            destination.Items.Add(item);
        }
        while (source.SelectedItems.Count > 0)
        {
            source.Items.Remove(source.SelectedItems[0]);
        }
    }
返回语句后有代码。当返回退出该方法时,将永远无法到达该代码


考虑移动ReplaceListBoxItemslistBox1、listBox2;高于回报

这是因为在这些行之前使用了return关键字。这种风格可能会有帮助

ReplaceListBoxItems(listBox1,listBox2);
return current + "      "+"Time In : "; // if the last "out" comes after the last "in"

只需替换以下行,而不是

         if (indexOut > indexIn)
          {
            ReplaceListBoxItems(listBox1,listBox2);
            return current + "      "+"Time In : ";// if the last "out" comes after the last "in"
          }
          else
          {
                // If the last "in" comes after the last "out"
                MoveListBoxItems(listBox1,listBox2);
                return current + "      " +"Time Out : ";
          }

非常感谢大家!在命中代码的这一部分之前返回。在这些行之前返回。