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
It sounds like you need to create a wrapper around an instance you can invalidate:
public class Ref where T : class
{
private T instance;
public Ref(T instance)
{
this.instance = instance;
}
public static implicit operator Ref(T inner)
{
return new Ref(inner);
}
public void Delete()
{
this.instance = null;
}
public T Instance
{
get { return this.instance; }
}
}
and you can use it like:
Ref carRef = new Car();
carRef.Delete();
var car = carRef.Instance; //car is null
Be aware however that if any code saves the inner value in a variable, this will not be invalidated by calling Delete
.