Hello.
In this lecture, we'll talk about question Tan.
What is the difference between this boss and finalized methods?
The dispose method is used to free unmanaged resources.
The finalized method is the same thing as the distractor, so it's the method that is caught on an object
when it is being cleaned up by the garbage collector.
First, let's focus on the displacement that this method comes from a disposable interface, and it
is used to release and unmanaged resources used by an object when its work is finished.
First of all, let's understand what managed and unmanaged resources are managed.
The resources, as their name suggests, are managed by the common language runtime, and the objects
you create with C-sharp are managed resources.
The garbage collector is aware of their existence, and once they are no longer needed, it will free
up the memory they occupy.
This means we don't need to worry about managing resources cleanup as it is done automatically.
For us, unlimited resources are beyond the realm of the C.R..
The garbage collector doesn't know about them, so it will not perform and the cleanup on them.
Examples of unlimited resources.
Our database connections.
File hangers come objects.
Opened.
Network connections, etc. We as developers are responsible to perform the cleanup after we are done
with those objects.
If we don't, but things may happen.
For example, if we open a file to read it and we don't close it, the next attempt to open the same
file will fail with an error, saying that the file is currently in use.
If we have a class that uses some unmanaged resources, it should implement the AI disposable interface
and provide an implementation of the dispose method.
The disposal method should contain the code that cleans the unmanaged resource.
For example, closes a file.
Let's see a simple cross that does so.
This glass is simply providing a way to eat foul line by line.
Please note that this implementation is simplified for example sake, so it doesn't contain an error.
Handling this class implements a disposable interface, and because of that, it is forced to provide
the implementation of the dispose method.
In this method, we clean up and unmanaged resources.
In this case, we simply called this prismatic on the stream reader.
This is a very common practice when implementing the dispose method because it rarely happens that we
need to access on the managed resources that do not have and this issue class needs to use them provided.
All right, let's use the Federal Reserve class.
At the first glance, it may seem OK, line one and line two are set to those coming from the input
text file.
The problem here is that we don't actually call Did this post mattered and this 300ER is never closed.
Let's fix that.
We can call the displacement of manually.
But this is a bit awkward and easy to forget, not to mention that exception will be thrown in this
court.
The post method will never be called.
It's better to use the U.S. statement.
Starting with the shop old, we can use the following syntax we felt braces.
Remember, the using statement is just syntactic sugar for this.
The final block is used to ensure that the dispose method will be called, no matter if the exception
will be thrown or not.
You can learn more about the using statement in the what is the use of the using keyword lecture?
One more thing before we move on to the finalized method, remember that the garbage collector does
not call did this post method?
It's not even aware of it.
We must call it ourselves, and the best way to do it is by using the using statement, which ensures
that the response method will be called.
Now, let's move on to the finalized method.
This method is called on an object when it is being cleaned up by the garbage collector.
This means it can only be added to reference types.
So, for example, a struct can't have a finalized or defined piece notice that in C-sharp the destructor,
the final riser and the finalized method are the same things.
We can't even define the finalized method in the class.
We must do so by defining the structure.
Let's see this in practice.
As you can see, declaring the finalized method doesn't work as the process does not overwrite object
finalize.
Instead, provide the structure.
Let's do it.
And purity, as you can see, the destructor doesn't have an access modifiers or return type.
And it is preceded by the tilde symbol.
All right.
So we defined a destructor in the person cross when an object of this class will be cleaned up by the
garbage collector.
This method will be executed.
Let me run some code that shows that.
John Object leaves in the scope of some method, and after this method finishes, it is no longer needed
by running GC correct command.
I can ask the garbage collector to do its work.
Let's see the result of this program.
As you can see, the destructor has been executed.
We now know how to define these structures.
So it's time to learn when to use them.
Well, the answer is almost never.
And then quoting Eric Lippert, one of the designers of C-sharp.
As we noted before, if a class is using some unmanaged resources that must be cleaned up, it should
implement a disposable interface.
Some people think that having a destructor that caused the dispose method can be an assurance that those
resources will be cleaned up.
If someone forgets to call the disbursement that manually or if they're using statement.
But this is solving an issue that shouldn't happen at all if developers know what they're doing.
So in the process, we may cause much more problems.
Let me quote Mr. Lippert again if you make a destructor, be extremely careful and understand how the
garbage collector works.
The structures are really weird.
They don't run on your thread.
They run their own thread.
Don't cause deadlocks on 100 exception thrown from a distractor is bad news.
It's only its second thread.
Who's going to catch it?
Destruct or may never run.
You can't rely on the object ever being scheduled for finalization.
It probably will be, but that's not a guarantee.
Almost nothing that is normally true is true.
This to be really, really careful.
Writing a correct destructor is very difficult, actually.
Eric Report mentioned that the only scenario when he needed to actually write these tractors was when
testing the part of the compiler that handles these tractors.
She never intended to do so in production code.
If you want to learn more about the tricky beasts that these structures are, I think some resources
in the file attached to this lecture.
So the bottom line here is do not raise this tractors.
If your objects must clean up some resources after they finish their work, make them implement the
widest possible interface.
Let's summarize the this POS method is used to free unmanaged resources.
The finalized method is the same thing as the destructor, so it's the method that is scored on an object
when it is being cleaned up by the garbage collector.
The topic of the dispose and finalize method is pretty common during the interviews, so you may hear
questions like what is the difference between a destruct or a final laser under the finalized method?
There is no difference as they are the same thing.
During the compilation, the destructor gets turned to the finalized method, which is commonly called
a Finisar.
Thus, the garbage collector call the displacement it.
No, the garbage collector is not aware of this method.
We must call it ourselves, usually by using the using statement When should we write our own these
structures?
The safest answer is almost never.
These structures are very tricky, and we don't even have a guarantee that they will run.
We should use the deports method instead.
What are managed and unmanaged resources?
The managed resources are managed by the common language runtime or objects created with C-sharp and
managed resources.
The garbage collector is aware of their existence and it will clean up the memory they occupy once they
are not needed.
We don't need to worry about human resources when it comes to cleanup and manage.
Resources are beyond the scope of the CRL are the garbage collector doesn't know about them and it will
not release them on.
The manage resources are things like database connections or file handlers.
We, as developers are responsible to perform the cleanup after we are done working with unmanaged resources.
All right, that's it for this lecture.
Thanks for watching and see you in the next one.