考虑以下代码:
public class Class1
{
public static int c;
~Class1()
{
c++;
}
}
public class Class2
{
public static void Main()
{
{
var c1=new Class1();
//c1=null; // If this line is not commented out, at the Console.WriteLine call, it prints 1.
}
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine(Class1.c); // prints 0
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,即使main方法中的变量c1超出范围并且在GC.Collect()调用时没有被任何其他对象进一步引用,为什么它没有在那里完成?
我有一个程序打开一个Excel COM对象,做一些东西,并关闭它.然后我想在文件关闭后移动它.如果我运行没有断点的程序,这可以正常工作.但是,如果我在尝试移动文件之前进入调试模式,我会得到一个IOException:"进程无法访问该文件,因为它正被另一个进程使用."
那是什么交易?当一个程序被允许以全速运行而不是在我踩过它时,垃圾收集是否表现更好?踩着我的代码做的不仅仅是非常缓慢地运行它吗?调试模式还有其他后果吗?遇到的其他错误只是因为我在调试而没有运行exe?