我正试图调用这些函数来摆脱我不需要的东西,但是我的代码似乎在我开始认为是徒劳的斗争中击败了我。 我已经尝试了多种方法来解决这两个最后的错误,但是IEnumerator却给了我通配符。 错误在线:if(enumerator2 is IDisposable)和if(enumerator is IDisposable)
try { enumerator = matchs.GetEnumerator(); while (enumerator.MoveNext()) { IEnumerator enumerator2; Match current = (Match) enumerator.Current; num = 0; try { enumerator2 = current.Groups.GetEnumerator(); while (enumerator2.MoveNext()) { Group group = (Group) enumerator2.Current; strArray[num, num3] = group.Value; num++; } } finally { if (enumerator2 is IDisposable) { (enumerator2 as IDisposable).Dispose(); } } if (num2 < num3) { num2 = num3; } num3++; } } finally { if (enumerator is IDisposable) { (enumerator as IDisposable).Dispose(); } } strArray[num, 0] = Conversions.ToString((int) (num2 + 1)); return strArray; }编辑 - 特定错误消息:使用未分配的局部变量'enumerator2' 使用未分配的局部变量“枚举器”
I am trying to call these functions to get rid of stuff I don't need, but my code seems to be defeating me in what I am begining to perceive to be a vain struggle. I have tried multiple ways to solve these last two errors, but the IEnumerator is giving me wild cards. errors are on the lines : if (enumerator2 is IDisposable) and if (enumerator is IDisposable)
try { enumerator = matchs.GetEnumerator(); while (enumerator.MoveNext()) { IEnumerator enumerator2; Match current = (Match) enumerator.Current; num = 0; try { enumerator2 = current.Groups.GetEnumerator(); while (enumerator2.MoveNext()) { Group group = (Group) enumerator2.Current; strArray[num, num3] = group.Value; num++; } } finally { if (enumerator2 is IDisposable) { (enumerator2 as IDisposable).Dispose(); } } if (num2 < num3) { num2 = num3; } num3++; } } finally { if (enumerator is IDisposable) { (enumerator as IDisposable).Dispose(); } } strArray[num, 0] = Conversions.ToString((int) (num2 + 1)); return strArray; }Edit- Specific error messages: Use of unassigned local variable 'enumerator2' Use of unassigned local variable 'enumerator'
最满意答案
正如其他人指出的那样,您应该使用foreach循环。
但是 ,你得到这个错误的原因是因为当finally块中使用了enumerator2时,编译器无法知道它被设置为某个值(因为它可能不是在某些特殊情况下)。 您可以按照原样修复您的代码:
IEnumerator enumerator2 = null; Match current = ...You should use a foreach loop as pointed out by others.
But, the reason you are getting that error is because when enumerator2 is used in the finally block, the compiler cannot know that it gets set to some value (because it may not be in some exceptional situations). You can fix your code as-is, by doing:
IEnumerator enumerator2 = null; Match current = ...更多推荐
发布评论