It has automatic collection, but it also uses a lot of manual techniques. For example it has using clauses, and it also follows a Dispose pattern. The need for Dispose in frameworks is due to not being able to "automatically" handle object disposal.
C++ uses a different pattern where objects can be disposed. It's got it's own problems of course, I just want to contradict the "best of both worlds" statement. :P
The need for Dispose in frameworks is due to not being able to "automatically" handle object disposal.
I'm not sure if that's right. Garbage Collection is automatic. Disposing is usually done manually. Because the whole point of disposables is to control when the object resources are disposed.
Closing files for example. You'd want to do that manually. Why would one wait for a garbage collector to do it?
It's considered one of the strong points of C++ that you don't need to manually cleanup resources of any kind, it doesn't make memory a special case.
The Dispose pattern is a contrast to this. Like C, all resources, except memory, must be manually released. Automatic "memory" management doesn't solve any of the problems of other resources, something that C++ does actually solve.
All resources are part of this discussion, and if we just ignore them it isn't a fair comparison between how languages handle it.
C# doesn't the C++ concept of automatic storage. In C# everything that's a reference-type is garbage collected. Garbage collection means that you don't have control over when the object is disposed. It doesn't generally matter, but sometimes it does. Like in files for example. This is where the disposable pattern comes in.
C++ doesn't have a runtime garbage collector. So things are different there. One can tell when a resource is going to be disposed (i.e. when an object's destructor will be called) unlike in C#. So there's no need for a disposable pattern here.
The .NET dispose pattern and the using remind me of Python's context managers and the with statement.
with in Python automatically calls an __enter__ and __exit__ method which means that the writer of the class can initialize and dispose resources.
As mostly everything in Python it does not uses interfaces but duck typing, so to adhere to the "context manager pattern" you just need to implement those two methods.
In practice it is used for file handling, connection management (mostly databases) and resource disposal.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It has automatic collection, but it also uses a lot of manual techniques. For example it has
using
clauses, and it also follows aDispose
pattern. The need forDispose
in frameworks is due to not being able to "automatically" handle object disposal.C++ uses a different pattern where objects can be disposed. It's got it's own problems of course, I just want to contradict the "best of both worlds" statement. :P
I'm not sure if that's right. Garbage Collection is automatic. Disposing is usually done manually. Because the whole point of disposables is to control when the object resources are disposed.
Closing files for example. You'd want to do that manually. Why would one wait for a garbage collector to do it?
It's considered one of the strong points of C++ that you don't need to manually cleanup resources of any kind, it doesn't make memory a special case.
The Dispose pattern is a contrast to this. Like C, all resources, except memory, must be manually released. Automatic "memory" management doesn't solve any of the problems of other resources, something that C++ does actually solve.
All resources are part of this discussion, and if we just ignore them it isn't a fair comparison between how languages handle it.
aaah you're contrasting it with C++. Makes sense.
C# doesn't the C++ concept of automatic storage. In C# everything that's a reference-type is garbage collected. Garbage collection means that you don't have control over when the object is disposed. It doesn't generally matter, but sometimes it does. Like in files for example. This is where the disposable pattern comes in.
C++ doesn't have a runtime garbage collector. So things are different there. One can tell when a resource is going to be disposed (i.e. when an object's destructor will be called) unlike in C#. So there's no need for a disposable pattern here.
Well the Dispose pattern is commonly used with unmanaged code.
The using clauses use the Dispose pattern.
See Dispose pattern docs on microsoft.docs
and wiki page
For me is the "best of both worlds". I didnt claim that this is what others think about the management of the memory in C#
The .NET dispose pattern and the
using
remind me of Python's context managers and the with statement.with
in Python automatically calls an__enter__
and__exit__
method which means that the writer of the class can initialize and dispose resources.As mostly everything in Python it does not uses interfaces but duck typing, so to adhere to the "context manager pattern" you just need to implement those two methods.
In practice it is used for file handling, connection management (mostly databases) and resource disposal.