| ▲ | Memory Safety in C# vs. Rust | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 points by northlondoner a day ago | 12 comments | |||||||||||||||||||||||||||||||||||||||||||||||
Noticed how C# is underrated. About memory safety in C#. How difficult to introduce multi-paradigm memory safety approach like Rust? Ownership model for example, would it be possible to enforce practice via some-sort of meta framework? | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | zamalek a day ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
C# is already memory safe. This isn't the reason why some people chose Rust over C#. | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | jasonthorsness a day ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
C#'s runtime (dotnet runtime) adds overhead compared to Rust with GC and other stuff too. This is true even with single-binary AOT compilation, the runtime is still there (just like Go). So it will never be suitable for some scenarios. You can definitely implement manual ownership tracking in C#, this is quite common for non-memory resources and does have some language syntactic sugar with the Dispose pattern for example. But you can't truly roll your own memory management/ownership unless you do something with "unsafe" which seems counter-productive in this case :P. | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | exceptione a day ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
neonsunset shared an interesting article, but his comment is dead: https://em-tg.github.io/csborrow/ /btw, I am not affiliated with neonsunset, but could people please comment to explain what is wrong instead of downvoting? If there is any substance to a comment and it isn't obviously a misinformation or disinformation one (Paradox of Tolerance), we should have a discussion instead. On topic: Could F* unlock even more possibilities, like crossing the gap of the heap and the stack in terms of direct access? It has a very powerful type system and it can eject an F* program to F#. | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | neonsunset a day ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||
C# actually already has limited lifetime analysis :) https://em-tg.github.io/csborrow/ > Ownership model for example, would it be possible to enforce practice via some-sort of meta framework? It should be possible to at least write an analyzer which will be based on IDisposable-ness of types to drive this. Notably, it is not always more efficient to malloc and free versus using GC, and generational moving GCs do not operate on "single" objects allocating and freeing them, no, so you cannot "free" memory either (and it's a good thing - collection is marking of live objects and everything unused can be reclaimed in a single step). Also the underlying type system and what bytecode allows is quite a bit more powerful than what C# makes use of, so a third language targeting .NET could also yield a better performance baseline by better utilizing existing (very powerful) runtime implementation. Lastly, there have been many improvements around devirt and object escape analysis, and GC changes are also a moving target (thanks to Satori GC), so .NET is in quite a good spot and many historical problems were or are in the process of being solved, that make Rust-style memory management less necessary (given in Rust you also make use of it because you want to be able to run your code on bare metal or without GC at all, only relying on host-provided allocator - if you do not have such requirement, you have plenty of more convenient options). | |||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | worldsavior a day ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||||||||
C# is underrated because it only works well on Windows and has bad frameworks such as .NET. There isn't really any reason to use it outside of developing Windows native applications. There are much better cross-platform languages, with a bigger community and better support. | |||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||