清理IDisposable问题(Cleaning up with IDisposable issues)

编程入门 行业动态 更新时间:2024-10-17 17:24:00
清理IDisposable问题(Cleaning up with IDisposable issues)

我正试图调用这些函数来摆脱我不需要的东西,但是我的代码似乎在我开始认为是徒劳的斗争中击败了我。 我已经尝试了多种方法来解决这两个最后的错误,但是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 = ...

更多推荐

本文发布于:2023-07-16 12:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:IDisposable   Cleaning   issues

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!