How to delete object?

前端 未结 9 1255
眼角桃花
眼角桃花 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条回答
  • 2021-02-13 18:43

    I would suggest , to use .Net's IDisposable interface if your are thinking of to release instance after its usage.

    See a sample implementation below.

    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;
        }
    
       }
    }
    

    Now in your code:

    void main()
    {
       using(var car = new Car())
       {
         // do something with car
       } // here dispose will automtically get called. 
    }
    
    0 讨论(0)
  • 2021-02-13 18:44

    Use a collection that is a static property of your Car class. Every time you create a new instance of a Car, store the reference in this collection.

    To destroy all Cars, just set all items to null.

    0 讨论(0)
  • 2021-02-13 18:48

    What you're asking is not possible. There is no mechanism in .Net that would set all references to some object to null.

    And I think that the fact that you're trying to do this indicates some sort of design problem. You should probably think about the underlying problem and solve it in another way (the other answers here suggest some options).

    0 讨论(0)
  • 2021-02-13 18:52

    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
    }
    

    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;
    

    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.

    0 讨论(0)
  • 2021-02-13 18:54

    You can proxyfy references to your object with, for example, dictionary singleton. You may store not object, but its ID or hash and access it trought the dictionary. Then when you need to remove the object you set value for its key to null.

    0 讨论(0)
  • 2021-02-13 19:01

    FLCL's idea is very correct, I show you in a code:

        public class O1<T> where T: class
        {
            public Guid Id { get; }
            public O1(Guid id)
            {
                Id = id;
            }
            public bool IsNull => !GlobalHolder.Holder.ContainsKey(Id);
            public T Val => GlobalHolder.Holder.ContainsKey(Id) ? (T)GlobalHolder.Holder[Id] : null;
        }
        public class GlobalHolder
        {
            public static readonly Dictionary<Guid, object> Holder = new Dictionary<Guid, object>();
            public static O1<T> Instantiate<T>() where T: class, new()
            {
                var a = new T();
                var nguid = Guid.NewGuid();
                var b = new O1<T>(nguid);
                Holder[nguid] = a;
                return b;
            }
            public static void Destroy<T>(O1<T> obj) where T: class
            {
                Holder.Remove(obj.Id);
            }
        }
    
        public class Animal
        {
    
        }
    
        public class AnimalTest
        {
            public static void Test()
            {
                var tom = GlobalHolder.Instantiate<Animal>();
                var duplicateTomReference = tom;
                GlobalHolder.Destroy(tom);
                Console.WriteLine($"{duplicateTomReference.IsNull}");
                // prints true
            }
        }
    

    Note: In this code sample, my naming convention comes from Unity.

    0 讨论(0)
提交回复
热议问题