Which method is used to enforce garbage collection in .NET?

System.GC.Collect() method is used to enforce garbage collection in .Net.

In .NET, you cannot directly enforce garbage collection. Garbage collection is managed automatically by the runtime, and as a developer, you typically don’t need to intervene. However, you can suggest to the garbage collector that now might be a good time to perform garbage collection by calling the System.GC.Collect() method.

However, it’s generally not recommended to call GC.Collect() explicitly in production code because it can negatively impact performance and disrupt the natural behavior of the garbage collector, which is optimized to run at appropriate times based on memory pressure and other factors.

So, the answer to the question “Which method is used to enforce garbage collection in .NET?” is System.GC.Collect(), but it’s important to understand that it’s not usually a best practice to call this method.