Does C# have any kind of GC behaviour guarantees that are similar?
Does C# have any kind of GC behaviour guarantees that are similar?
In case you forget the GC will call Dispose but it's entirely up to the GC when that happens, so personally I wouldn't say it's similar.
[1]: https://learn.microsoft.com/en-us/dotnet/fundamentals/runtim...
[2]: https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
This actually has to be implement in the classes finalizer its not done automatically. The finalizer is called by the GC which if the designer chooses can call Dispose. This is usually done in well designed classes that use unmanaged resources.
Dispose and using give you the determinism if you want it and the finalizer gives you the backstop to prevent leaks if it makes sense.
So it's flexible, but there are footguns about, especially if you're wrapping a handle to something external.
1. https://learn.microsoft.com/en-us/dotnet/api/system.idisposa...
2. https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
I think you are making a point in the broader discussion of C# vs. rust, but that doesn't fit here. (And, personally, I don't care about that so have nothing to say on the topic.)