C# Unreachable code detected

后端 未结 5 2594
抹茶落季
抹茶落季 2021-02-20 18:57

I\'m getting a \"Unreachable code detected\" message in Visual Studio at the point i++ in my code below. Can you spot what I\'ve done wrong?

try
{
    RegistryKe         


        
5条回答
  •  庸人自扰
    2021-02-20 19:26

    The problem is that this actually isn't a loop. You don't have any condition on the break so you could equivalently write something like

    if(cmbPath.Items.Count > 0)
    {
       OurKey.SetValue("paths" + 0, cmbPaths.Items[0]);
    }
    

    Alternatively you have to correct with something like

    for (int i = 0; i < cmbPaths.Items.Count; i++) 
    {
       OurKey.SetValue("paths" + i, cmbPaths.Items[i]);
    
       if(someConditionHolds)
          break;
    }
    

提交回复
热议问题