How to delete an object?

I need to create a class method that deletes an 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 it.

An instance of this class will be stored in hundreds of different classes. I will try to describe this, for example, the following classes will be:

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];
     }
}
+4
source share
8 answers

From any class, you cannot set its value to null. This is unacceptable and does not make sense either -

public void Delete()
{
    this = null; <-- NOT ALLOWED
}

You need an instance of the class to call the Delete () method, so why not set this instance to zero as soon as you are done with it.

Car car = new Car();
// Use car objects and once done set back to null
car = null;

, , , #. , , , Car . , GC .

+11

.Net IDisposable, .

. .

public class Car : IDisposable
{

   public void Dispose()
   {  
      Dispose(true);
       // any other managed resource cleanups you can do here
       Gc.SuppressFinalize(this);
   }
   ~Car()      // finalizer
   {
        Dispose(false);
   }

   protected virtual void Dispose(bool disposing)
   {
     if (!_disposed)
     {
      if (disposing)
      {
        if (_stream != null) _stream.Dispose(); // say you have to dispose a stream
      }

      _stream = null;
    _disposed = true;
    }

   }
}

:

void main()
{
   using(var car = new Car())
   {
     // do something with car
   } // here dispose will automtically get called. 
}
+4

, , :

public class Ref<T> where T : class
{
    private T instance;
    public Ref(T instance)
    {
        this.instance = instance;
    }

    public static implicit operator Ref<T>(T inner)
    {
        return new Ref<T>(inner);
    }

    public void Delete()
    {
        this.instance = null;
    }

    public T Instance
    {
        get { return this.instance; }
    }
}

:

Ref<Car> carRef = new Car();
carRef.Delete();
var car = carRef.Instance;     //car is null

, - , , Delete.

+4

, , . .Net , null.

, , , - . , - ( ).

+3

#. MANAGED. ( ++).

, null. , GC ( ), , Collect. , , .

, ? : GC , , , .

When you set an instance to null you just notify that your object has no referene anymore ant that could help CG to collect it faster !!!
+1

, Car. , Car, .

Car s, null.

+1

, , singleton. , , . , , null.

0

.

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

. GC . : Car.DeleteObj()

, / , .

-1

All Articles