First of all, Dispose
does not make "memory release". The method serves to finalize resources used.
For example: a file that has been opened for editing or a network connection.
Briefly, when a program reads/writes to disk, makes a connection to the network, among other operations, it is necessary to use operating system resources making system calls. Therefore, when finished using these resources, they need to be closed/finished and this is what the method Dispose
does - or at least should do, because it will always depend on the implementation.
Memory release is something that has nothing to do with it, when you create a variable, you are allocating a space in memory and when this variable no longer needs to be used, this space can be released and give way to another data. In normal cases, you don’t have to worry about doing memory management because in an application. NET Garbage Collector does this automatically.
Knowing this then, there is some difference in performance in compilation time since everything will be transformed into Try/catch? What would be ideal?
You may have, I don’t think you can even measure effectively. But it’s certainly not something you should be worried about.
If there is a difference it must be something very small. And let’s face it, compromise the readability (or even the guarantee of the method call Dispose()
) because of milliseconds (or micro?) less in the compilation does not seem to me a good exchange.
What would be the best practice for the release of both memory and resource? I must set the variable = null even being inside the using block to ensure the 2?
"Better" change context to context, there is no cake recipe.
However, set the variable to null
you won’t have no effect. The only thing that will happen is that you will be spelling out the end of the use of that reference. What will probably already happen in some instructions after you do this assignment (for example: end of method for local variables).
Besides being useless, it ends up facilitating the introduction of bugs if someone tries to use that variable later.
What do you mean by the following excerpt: "But in practice we know it can be a little different, once in performance another to avoid Code Smells."?
– Jéf Bueno
I may have expressed myself badly about the text.
– Victor Laio
I would like to point out that I was reading the Dispose Pattern code before it was removed in the face of rain of negativity. To the author, who does not know the name, I ask you to post a comment with the link.
– Augusto Vasques
Hardly remains to say anything more that has not already been said in all these questions: https://answall.com/search?tab=votes&q=dispose.
– Maniero