更新时间:2023-12-05 08:51:52
在任何类中,您都不能将其值设置为null.不允许,也没有意义-
From any class you can't set its value to null. This is not allowed and doesn't make sense also -
public void Delete()
{
this = null; <-- NOT ALLOWED
}
您需要一个类的实例来调用Delete()方法,所以一旦完成该操作,为什么不将该实例设置为null呢?
You need an instance of class to call Delete() method so why not set that instance to null itself once you are done with it.
Car car = new Car();
// Use car objects and once done set back to null
car = null;
在C#中,无论如何您都无法实现.我猜测 从您的问题中得出您想要的原因,因为存在内存泄漏 当前设计中存在的问题,该问题不允许Car实例 走开.我建议您更好地描述您的应用程序, 确定阻止GC收集汽车实例的区域,并 致力于改善这一领域.
Anyhow what you are trying to achieve is not possible in C#. I suspect from your question that you want this because there are memory leaks present in your current design which doesn't let the Car instance to go away. I would suggest you better profile your application and identify the areas which is stopping GC to collect car instance and work on improving that area.