Is it necessary to unsubscribe from event on Dispose() in this case?

classA creates an instance of classB, stores the classB instance as a field, and subscribes to a classB generated event.

ClassA is IDisposable. ClassB is not IDisposable.

On dispose of classA, is it necessary for classA to unsubscribe from the classB event?
deleydSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
If an object is disposable, it is really a good practice to call the Dispose method (or to bundle the usage with a USING structure).
0
it_saigeDeveloperCommented:
It is also generally good practice to unsubscribe from events when you no longer will use them.

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deleydSoftware EngineerAuthor Commented:
Is it necessary to unsubscribe if classB is one classA created, and classA is being disposed? Will the reference held by classA prevent classB from being garbage collected?
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

it_saigeDeveloperCommented:
No it won't but say that the subscription to classB's event uses unmanaged resources, you already stated that classB does not implement IDisposable, so the only way to sensible way to free those unmanaged resources would be by unsubscribing the event, as long as the event unsubscription process handles the freeing of those unmanaged resources, which from a design standpoint, is sensible.

-saige-
0
deleydSoftware EngineerAuthor Commented:
How would I determine if "the subscription to classB's event uses unmanaged resources"?

It's just something like (in ClassA):
classB.MyEvent += this.MyEventHandler;

Open in new window

0
it_saigeDeveloperCommented:
You would have to review the code of classB in order to determine if it used any unmanaged resources.

-saige-
0
deleydSoftware EngineerAuthor Commented:
ClassB is not IDisposable.
0
it_saigeDeveloperCommented:
A class, correction; an Object, does not *have* to implement IDisposable in order to consume or declare unmanaged resources.

Another way to look at it, regardless of the implementation, it's just a good practice.

-saige-
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.