如何删除对象? [英] How to delete object?

查看:106
本文介绍了如何删除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个删除实例的类方法.

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_Color; }
        set { m_Color = value; }
    }

    public Car()
    {
    }

    public void Delete()
    {
        /*This method will delete the instance,
        so any references to this instance will be now null*/
    }
}

class Program
{
    static void Main( string[] args )
    {
        Car car = new Car();

        car.Delete();

        if(car==null)
            Console.WriteLine("It works.");
        else
            Console.WriteLine("It doesn't work.")
    }
}

我想知道是否有任何可行的解决方案(即使不建议这样做).

I want to know if there is any possible solution (even if it is not recommended) how to do this.

该类的实例将存储在数百个不同的类中.我将尝试描述这一点,例如将有以下类:

Instance of this class will be stored in hundreds of different class. I will try to describe this, for example there will be these classes:

public class CarKey
{
    private Car m_Car;

    public Car Car
    {
        get { return m_Car; }
    }

    public bool CarExist{ get{ return m_Car != null; } }

    public CarKey( Car car )
    {
        m_Car = car;
    }
}

public class Garages
{
     private List<Car> m_Collection = new List<Car>();

     private int m_Size;

     public int Size{ get{ return m_Size; } }

     public Garages( int size )
     {
         for(int i=0;i<size;i++)
             m_Collection.Add(null);
     }

     public bool IsEmpty( int garage )
     {
         return m_Collection[garage] == null;
     }

     public void InsertCar( Car car, int garage )
     {
          if( m_Collection[garage] != null )
            throw new Exception("This garage is full.");

          m_Collection[garage] = car;
     }


     public Car GetCar( int garage )
     {
         if( m_Collection[garage] == null )
           throw new Exception("There is no car, maybe it was deleted.");

         return m_Collection[garage];
     }
}

推荐答案

在任何类中,您都不能将其值设置为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.

这篇关于如何删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆