Passing a Derived Object as a Reference Parameter

B

Bruce

How can I create a method that will take any object derived from a
base class as a reference parameter?

Something like this:

void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}

DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);

Where derivedObject1, 2 and 3 are of a class type derived from
BaseObject?

Thanks....

--Bruce
 
J

Joanna Carter \(TeamB\)

Something like this:

void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}

DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);

Yes, but I have to ask why you are explicitly disposing of objects instead
of letting the garbage collector do its work ? Or do your classes hold
references to Windows resources or file handles?

Joanna
 
M

Mattias Sjögren

Where derivedObject1, 2 and 3 are of a class type derived from
BaseObject?

BaseObject bo = derivedObject1;
DestroyObject(ref bo);

But I don't understand why you use a ref parameter. If you used a by
value parameter, you could pass in derivedObject1 directly.



Mattias
 
R

Richard Blewett [DevelopMentor]

Dispose doesn't clean up the same resources as the GC. Its there to allow timely clean up of *non-memory* resources. The GC only cleans up memory. If an object implements IDisposable and you have ownership of the object then you should call dispose on it. You are confusing GC with finalization. Finalization is something that happens (hopefully) after the GC realizes a finalizable object is collectable.I say hopefully because it isn't guaranteed to run. You need to make your object finalizable (implement a C# destructor) if and only if you are managing non-managed resrouces like windows HANDLEs and the like. If you implement a finalizer then you should also implement IDisposable. However, there are good reasons to implement IDisposable even if you don;t imeplement a finalizere (for example you have memebers which themselves implement IDisposable).

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Something like this:

void DestroyObject(ref BaseObject obj)
{
obj.Dispose();
obj = null;
}

DestroyObject(ref derivedObject1);
DestroyObject(ref derivedObject2);
DestroyObject(ref derivedObject3);

Yes, but I have to ask why you are explicitly disposing of objects instead
of letting the garbage collector do its work ? Or do your classes hold
references to Windows resources or file handles?

Joanna
 
J

Jon Skeet [C# MVP]

Mattias Sjögren said:
BaseObject bo = derivedObject1;
DestroyObject(ref bo);

But I don't understand why you use a ref parameter. If you used a by
value parameter, you could pass in derivedObject1 directly.

I suspect the point is that the variable will be set to null
afterwards. I can't say it's a pattern I really like though...
 
J

Joanna Carter \(TeamB\)

Dispose doesn't clean up the same resources as the GC.

That was what I was trying to say in the last sentence of my post; you just
said it better :))

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top