Ivan Cavalheiro
Ivan Cavalheiro

Reputation: 109

Set a reference to null in c#

I've been looking for this info for a loooong time. Never seen anyone asking stuff like this before. I know that we can use pointer in C# inside unsafe code, but thats not the case. The problem is:

MyClass beta = new MyClass();
MyClass alpha = beta;
beta = null;

What I need to do now is set beta to null and ALSO set alpha to null. Let's say that i have returned alpha in a function, so I dont have access to it anymore, so, how can I set it to null? In c++ if beta and alpha were pointer I could just free the memory that beta points to, but I dont know a way to do that in c#.

In a more basic way, lets say that I have to set both variables to null, but i have acces only to one of them.


Edit

I did see some answers that have nothing to deal with the question i did... Ill try to explain again. I'm creating a class that do some motion effect and return a instance that can be used to pause or stop this motion. The programmers that use this class are used to test variables with if(variable != null) before using it. What i need is when the motion is over the instance they have is turned into null, so they know that its not usefull anymore.

Is there any way of doing it?

Upvotes: 4

Views: 5649

Answers (4)

Patrick M
Patrick M

Reputation: 10989

You don't need to do either. C# memory is automatically managed, so things get freed when there are no more references to them. If you specifically want to free something because you've caught an OutOfMemoryException, then yes, you can start setting things to null and forcing the garbage collector as @JanesAbouChleih points out. It is much better to call the .Dispose method and implement any other clean up code MyClass may require (closing files, flushing buffers, etc.).

Some references:

http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

http://msdn.microsoft.com/en-us/library/ms973837.aspx

Also, similar question on how/when/why to dispose things: Do you need to dispose of objects and set them to null?

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100545

If you need to get some sort of AV/exception (to match C++ behavior of accessing deleted object) the closest you can get is make object IDisposable and make all methods/properties to thrown after object is disposed (similar to how Stream-derived objects behave).

MyClass beta = new MyClass();
MyClass alpha = beta;
beta.Dispose();

alpha.Method(); // should throw  ObjectDisposedException.

Upvotes: 0

abc123
abc123

Reputation: 18823

Unless your class contains a disposable object there isn't a need to Dispose of the object, and setting it to null simply makes the object null which doesn't free memory but instead would help if you need to check the object later...

If your class needs to be marked as disposable because it needs to clean up it's memory usage please look into making your class inherit from the IDisposable! interface.

Upvotes: 0

Servy
Servy

Reputation: 203837

You need to add an additional layer of indirection. To put things in C++ terms, MyClass is a pointer to an object, so to set both pointers to null you need to be passing around pointers to pointers instead, so that you can resolve the pointer to pointer to just a pointer, set that to null, and both double pointers will resolve to null.

Adding an extra layer of indirection in C# usually means creating a new class.

public class Wrapper<T>
{
    public T Value { get; set; }
}

Using that, we can make alpha and beta each be Wrapper<MyClass> objects (make sure they are both the same instance of Wrapper), and to affect both "pointers" you can just set Value of the Wrapper to null.

Upvotes: 10

Related Questions