How to "clear" an object?

What is the best way to clear an object:

1) Write a method inside the class that clears all members of the class: MyObject.Clear();

2) Inside my code, in business logic, a null object: MyObject = null;

+4
source share
5 answers

They do different things. Setting a variable to null does nothing for the object itself. This may mean that the object now has the right to garbage collection, but it does not matter if there is another “live” link to the same object.

What should you do? Well, it totally depends on your situation. If you have many different places with reference to the same object, and all of them should see a "gap", then change the object. If you just need one variable to reference another object or no object at all, just change the value of the variable.

It is important to know the difference between them - it is very important to know that the value of a variable is never an object ... it is either a reference value or a value type value.

+12
source

I would recommend just creating a new instance of the object, rather than clearing it.

If you are finished with an object, but not a class containing its reference, setting it to null may be appropriate. However, if it is used only in the method, you don’t need to do anything (the garbage collector will clean it someday after there are no references to the object) - just let it “go away”, allowing it to drop out the volume. If it is in a collection, you can simply remove it from the collection.

The only exception is if the object implements IDisposable . In this case, you can call the Dispose() method or try to structure your code to use it in the using block so that its resources are cleaned accordingly.

+3
source

Second. Be careful not to continue to reference your object. Any handlers of related events, links in collections make your instance "unsafe".

If you came from C ++, do not worry with the Garbage Collector. I know that a memory obsession can lead to manipulation of the data collection process. In fact, you will never need to use it. Just untie any links.

+1
source

The answer to your question: "It depends." It depends on what you are trying to do with it. If you want to keep a link to this particular instance (i.e. Others link to it or build it expensive), then you are probably best off using the Clear method. But only if you intend to continue using this particular instance of the object.

If you no longer need the object, just stop referencing it. In general, you do not need to set it to null , as the code generator can determine the lifetime of the object and make the object available for garbage collection. But if you want to make sure that the object is collected as soon as possible (i.e., during the next garbage collection cycle), then setting MyObject = null is the way to go. Again, that is usually not necessary, but it will not hurt anything.

0
source

I do not think so. While working with the C # course, we have GC (garbage collection) to push changes in this is very difficult, if you use C ++, it turned out to be quite easy

0
source

Source: https://habr.com/ru/post/1336050/


All Articles