Question

I have an object that is generated in one class

public class CreatingClass
{
    public T CreateObject<T>(Dictionary<string, object> parameters) where T : IMyInterface, new()
    {
        ....
    }

    public void DestroyObject(IMyInterface objectToDestroy)
    {
        ....
    }
}

I call this function from a client class, then at times need to nullify it through my application by the creating class.

Can I do something like the following

public class ClientClass
{
    MyObject obj;
    CreatingClass creatingClass = new CreatingClass();

    private void AFunctionToCreateMyClass()
    {
        obj = creatingClass.CreateObject<MyClass>(parameters);
    }

    private void AFunctionToDeleteMyObject()
    {
        CreatingClass.DestroyObject(obj);
        Assert.IsNull(obj);//Doesn't fail
    }
}

I had tried objectToDestroy = null, but didn't think it would work (and it didn't)

Was it helpful?

Solution

Note that you can't actually destroy an object; you are subject to the rules of garbage collection. At a push, you could check for IDisposable and call Dispose(),

You can use the ref samples provided, but I'm not sure there is much point; it is simpler just to clear the field with "obj = null;".

The ref usage could get confusing, since that works on the variable - i.e. if you do:

var tmp = obj;
DestroyObject(ref tmp);

then obj will still be the original value. Unless you have a good reason, I don't recommend the ref approach.

OTHER TIPS

What you want is

public void DestroyClass(ref IMyInterface objectToDestroy)
{
    ....
    objectToDestroy = null;
}

This is will set your local reference to null

All parameters in C# are passed by value, so if you want to modify the reference itself, you need to pass it using the ref keyword.

Are you looking for the IDisposable pattern?

[Your 'CreatingClass' is usually termed a factory]

Not sure why you'd be concerned with nullifying an object; it will be garbage collected only after all 'root' references to it have been removed. But if you change to:

 public void DestroyClass(ref IMyInterface objectToDestroy)   
 {       
     ....    
     objectToDestroy = null;
 }

and call as:

private void AFunctionToDeleteMyObject()    
{        
   CreatingClass.DestroyObject(ref obj);    
   Assert.IsNull(obj);  
}

A little clarification on the answers supplied...

Parameters in C# are passed by their type, value for value types, reference for reference types. You however, are passing an interface which could relate to a class (reference type) or struct (value type), therefore you need to explicitly declare it as a ref variable.

However, what you should really follow (as mentioned previously) is the IDisposable pattern, which was designed for this functionality.

EDIT: Parameters are passed by value, but for reference types the reference is the value. Therefore is you destroy the refence in the function, the original reference type is unaffected. If you make a change to the reference variable, i.e. modifying data in a dataset, the original reference type is updated outside of the function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top