对现有的对象C#的'新'的关键字 [英] C# The 'new' keyword on existing objects
问题描述
我想知道是什么发生了一个对象(在C#),一旦其参考变为重新分配。例如:
I was wondering as to what happens to an object (in C#), once its reference becomes reassigned. Example:
Car c = new Car("Red Car");
c = new Car("Blue Car");
由于参考重复使用,并垃圾收集处置/处理红车这是丢失后,它的参考?还是一个单独的方法需要落实处置红车的?
Since the reference was reused, does the garbage collector dispose / handle the 'Red Car' after it's lost its reference? Or does a separate method need to be implemented to dispose of the 'red car'?
我主要想知道,因为有我要去参加一个比较大的对象回收,并且需要知道,如果有什么事情,当它被重建了应该做的。
I'm primarily wondering because there's a relatively large object that I'm going to recycle, and need to know if there is anything that should be done when it gets recreated.
推荐答案
在你的榜样,在红车
实例ç
将成为符合垃圾收集时, C
分配给蓝色车
。你不需要做任何事情。
In your example, the Red Car
instance of c
will become eligible for garbage collection when c
is assigned to Blue Car
. You don't need to do anything.
看看这个(老了,但仍然相关)MSDN有关.NET垃圾收集器的文章。 http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
Check out this (old, but still relevant) MSDN article about the .NET garbage collector. http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
第一段说,这一切:
垃圾收集在Microsoft .NET公共语言运行时环境完全免除从跟踪内存使用情况,并知道何时释放内存的开发者。
Garbage collection in the Microsoft .NET common language runtime environment completely absolves the developer from tracking memory usage and knowing when to free memory.
这篇关于对现有的对象C#的'新'的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!