清理IDisposposable问题

| 我试图调用这些函数来摆脱我不需要的东西,但是我的代码似乎在我开始被认为是徒劳的斗争中击败了我。我尝试了多种方法来解决这最后两个错误,但是IEnumerator给了我通配符。错误即将发生:  如果(枚举数2是IDisposable) 和  如果(枚举数是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 \' 使用未分配的局部变量\'enumerator \'     
已邀请:
        您应该使用别人指出的“ 1”循环。 但是,收到该错误的原因是因为在
finally
块中使用
enumerator2
时,编译器无法知道它已设置为某个值(因为在某些特殊情况下可能不会)。您可以通过以下方式按原样修复代码:
IEnumerator enumerator2 = null;
Match current = ...
    
        使用for-each循环而不是while循环。如果需要,它将为您调用Dispose。 http://msdn.microsoft.com/zh-CN/library/aa288257(VS.71).aspx     

要回复问题请先登录注册