以下是一个c#代码,其中我必须在整个代码后面找出对象的数量(DOG)和引用。起初我计算它是4个对象和8个引用。但是在看到这个问题之后 Garbage collector test(这说垃圾收集器只有在系统内存不足和其他类似原因时才运行),我怀疑对象的数量是否应该是6 ?.下面的问题没有提到该程序获得的内存。c#代码中没有对象和引用,c中的垃圾回收#
所以我的问题是如果我得到这种类型的问题应该是我的答案(4,6或应该说这取决于内存)?
Dog rover = new Dog();
rover.Breed = “Greyhound”;
Dog rinTinTin = new Dog();
Dog fido = new Dog();
Dog quentin = fido;
Dog spot = new Dog();
spot.Breed = “Dachshund”;
spot = rover;
Dog lucky = new Dog();
lucky.Breed = “Beagle”;
Dog charlie = fido;
fido = rover;
rinTinTin = lucky;
Dog laverne = new Dog();
laverne.Breed = “pug”;
charlie = laverne;
lucky = rinTinTin;
//没有对象和参考的在这一点?(DOG对象不考虑字符串对象)
希望这问题不傻。
我认为你必须忽略这个问题的垃圾回收,因为它在运行时是不确定的。还要注意,字符串是对象,并且在您的示例代码中,“Dog.Breed”是对字符串的引用。 – 2015-02-11 09:25:49
@MthetheWWatson ....我只对狗物件感兴趣。但谢谢你指出。 – dividedbyzero 2015-02-11 09:29:46