3 ms·
Rust gives you clear semantics on when it drops a variable, so it's being used not only for memory safety but to close files, kill threads, synchronization prim
by k_bx 3y ago
Rust gives you clear semantics on when it drops a variable, so it's being used not only for memory safety but to close files, kill threads, synchronization primitives, and whatever else you want (can be implemented via Trait).
Does C# have any kind of GC behaviour guarantees that are similar?
- jmull 3y agoThat's great, but not relevant to the topic of memory safety in C#, which is all I was addressing. 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.)
- magicalhippo 3y agoNot really. There's the IDisposable interface you can implement, and some language features around making it easy to use[2], however there's nothing preventing you from not using those language features and forgetting to call Dispose. 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/runtime-libraries/system-idisposable https://learn.microsoft.com/en-us/dotnet/fundamentals/runtim... [2]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
- SigmundA 3y ago>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. 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.
- magicalhippo 3y agoGood point, I forgot about that little, but crucial, detail. So it's flexible, but there are footguns about, especially if you're wrapping a handle to something external.
- cerved 3y agoDoesn't the compiler complain if you don't use using or try? I forget
- SigmundA 3y agoC# is IDisposable [1] for that kind of stuff and has the "using" block [2] to make it hard to get wrong. 1. https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-8.0 https://learn.microsoft.com/en-us/dotnet/api/system.idisposa... 2. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
- deleted 3y ago[deleted]