开发者

can a enum variable garbage collected?

Normally all reference types are garbage collected when it is having no reference. What about a enum variable? will it be garbage collected even 开发者_如何学Cif it is out of scope(means it is not referenced more)?


Your question is very confusingly written.

Normally all reference types are garbage collected when it is having no reference.

Reference types are not collected. Instances of reference types are collected. And they are not collected when the instance has no references. They are collected when the garbage collector runs and determines that the referenced object cannot be reached from a known-to-be-alive root.

What about a enum variable? will it be garbage collected even if it is out of scope (means it is not referenced more)?

The scope of a variable is the region of program text in which the variable may be accessed by its name. The scope of a variable is only loosely connected to its lifetime, which is what you are asking about. The lifetime of a variable may be longer or shorter than the period of time that program control is in its scope!

Also, it is completely unclear what you mean by "will an enum variable be garbage collected?" Variables are storage locations, so the variable has to be somewhere. Where is the variable? Is it a field of a class? is it a local variable? Is it a closed-over outer variable of a lambda? Is it an element of an array of enumerated type? All of these affect whether the storage associated with the variable is collected, and if it is, when it is collected.

Your question doesn't really make any sense the way you've asked it. Can you clarify the question?

You might also want to read my answer to this related question:

Do value types get Garbage collected?


An enum is a value type variable so it is automatically released forgotten when it falls out of scope.


Enumeration in C# is a value type so will follow the rules of garbage collection for value types, which defers based on declaration scope of that type. You can find a lot information on internet. Here is an example link of reference and value types comparison also from GC perspective.

http://www.albahari.com/valuevsreftypes.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜