C# Unreachable code detected

安稳与你 提交于 2019-12-04 02:10:20

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

You're breaking out of the loop before the end of the first iteration.

Daniel Elliott

The problem is that because you break; in the loop with no chance of it doing anything else, the increment of i (i++) will never be reached.

Although your problem is solved i need to tell you this, you can just using the CreateSubKey() method for your purpose. I think It's a better choice. :)

//Creates a new subkey or opens an existing subkey for write access.
var ourKey = Registry.CurrentUser.CreateSubKey("Software\\Resources\\Shared");

You can also end up getting unreachable code if you use say for example Entity Framework, and you didn't add that reference to that project.

Say you have several projects like A Data Layer Project, a Domain Classes, then you create a console app for testing or whatever and you reference where your dbcontext is at, but if you don't use say nuget and add in EF, you will get code unreachable when trying to write a loop etc...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!