Add Manual Object Deletion Support in C# (like C++ delete) #9529
-
I’d like to propose or start a discussion around introducing an optional mechanism in C# to allow manual object deletion, similar to how delete works in C++. 💡 MotivationWhile the .NET garbage collector is excellent for most applications, there are advanced scenarios — such as game development, real-time systems, and memory-constrained environments — where manual memory control can be extremely beneficial. Examples include:Large object graphs that need to be cleared deterministically. Currently, the only way to influence cleanup is:Setting references to null Expecting:if we not use the delete keyword for the object, it will wand to clear automatically by GC (Safe) from memory leaks and if we use delete keyword for the object, that object wand to remove instantly from the memory. 🧪 Idea / SyntaxA possible (safe) syntax idea: delete obj; 🤔 Why Not Just Use Dispose()?Dispose() helps free unmanaged resources — but the managed object still lives in memory until the GC runs, which can lead to unpredictable spikes in high-performance apps. I'd love to hear thoughts from the community and the language team on whether this is feasible, valuable, or totally out of scope for C#. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Dupe of #7933 |
Beta Was this translation helpful? Give feedback.
-
If the problem is lots of objects of the same type being cycled, then an object pool where you borrow and return them without allocation is the best way I've found. |
Beta Was this translation helpful? Give feedback.
Dupe of #7933