English 中文(简体)
清理IDisposable问题
原标题:Cleaning up with IDisposable issues

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块中使用枚举器2时,编译器无法知道它被设置为某个值(因为在某些特殊情况下可能不会这样)。您可以按原样修复代码,方法是:

IEnumerator enumerator2 = null;
Match current = ...
问题回答

使用for each循环,而不是while循环。如果需要,它将为您调用Dispose。

http://msdn.microsoft.com/en-us/library/aa288257(VS.71).aspx





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签