4 ms·
> C# doesn't have ownership/lifetimes and the kind of safety that comes with them. That's not really true. Like rust, C# is memory safe, although it comes at
by jmull 3y ago
> C# doesn't have ownership/lifetimes and the kind of safety that comes with them.
That's not really true.
Like rust, C# is memory safe, although it comes at it in a very different way.
"safe" rust does inherently prevent a certain kind of race condition in multi-threaded code that C# does not, thought that's more of a nice incremental improvement, not a fundamental one -- i.e. it doesn't make your multi-threaded code thread-safe, but it does prevent a one type of thread safety violation.
- k_bx 3y agoRust 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]
- IshKebab 3y agoHe wasn't talking about memory safety. Rust's lifetime/ownership features were originally created to provide memory safety, but it turns out they actually provide more than that and are very good at preventing ordinary logic bugs too. I don't know if anyone has really investigated why this is the case but it definitely is.