|
|
|
|
|
by pjmlp
1672 days ago
|
|
C and C++ memory managements are alike, C# doesn't need reference counted library types. If you mean RAII, they can be easily done with IDisposable, using and Rosylin analysers that throw errors when usings are forgotten. And yes, manually memory management should be last resource, proven by profiler data. |
|
IDisposable is not a substitute for RAII, and C# has nothing that can manage ownership in equivalent ways as it lacks deterministic destruction. Implementing, for example, a data structure based on unmanaged memory in C# in such a way that it can be safely used by code outside the library without undermining the safety of the language (i.e. without introducing the possibility of programming errors outside the data structure implementation causing leaks or memory corruption) is an exercise in discipline and requires a thorough understanding of the runtime - eg. knowing that an object can be garbage collected while a method on it is executing. I know this because I've done it (as a last resort after extensive profiling and production use of various optimised, managed versions of the library).