How to delete object?

前端 未结 9 1356
眼角桃花
眼角桃花 2021-02-13 18:39

I need to create a method of class that delete the instance.

public class Car
{
    private string m_Color;

    public string Color
    {
        get { return m         


        
9条回答
  •  猫巷女王i
    2021-02-13 19:03

    You can use extension methods to achive this.

    public static ObjRemoverExtension {
        public static void DeleteObj(this T obj) where T: new()
        {
            obj = null;
        }
    }
    

    And then you just import it in a desired source file and use on any object. GC will collect it. Like this:Car.DeleteObj()

    EDIT Sorry didn't notice the method of class/all references part, but i'll leave it anyway.

提交回复
热议问题