Solved

Considerations when having IDisposable private member variables

Posted on 2016-09-24
4
43 Views
Last Modified: 2016-09-25
I have a class similar to below which has a private member variable whose type is from a 3rd party and implements IDisposable.  My question is are there any considerations I should be making when using a type this way?

    public class PaypalService
    {
        //PayPalAPIInterfaceClient implements IDispose
        private readonly PayPalAPIInterfaceClient _paypalClient = new PayPalAPIInterfaceClient();

        public List<PaypalTransaction> GetTransactions(DateTime startDate, DateTime endDate)
        {
            //do stuff using _paypalClient
        }
        public List<PaypalTransaction> GetTransactions(string payerEmail)
        {
            //do stuff using _paypalClient
        }
        public List<PaypalTransaction> GetTransaction(string transactionId)
        {
            //do stuff using _paypalClient
        }
    }

Open in new window



In all the code examples the class is wrapped in a using() similar to below which is what made me think I might be causing problems using it as a member variable.

            using (var paypalClient = new PayPalAPIInterfaceClient())
            {
                //do stuff using paypalClient
            }

Open in new window

0
Comment
Question by:canuckconsulting
  • 2
  • 2
4 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 41814051
If the disposable thing is at the class level, then your class needs to implement IDisposable so that other developers have the chance to dispose of the object. You will dispose of the class-level object in the Dispose method you create. Otherwise, if you're only using something disposable within a method, then wrap that usage in a using statement.
0
 

Author Comment

by:canuckconsulting
ID: 41814052
Hi käµfm³d.

It's used by multiple members within the class which is why I have it set as a member variable.

What would the IDisposable method look like?  Would it simply call _paypalClient .Dispose()?
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 41814063
Yes, but you should follow the disposable pattern when you implement it, as recommended by MS.

By creating a public Dispose method, you tell other developers that they should expect to clean up this class (via that method) when they are done with it.
0
 

Author Closing Comment

by:canuckconsulting
ID: 41814936
Great link.  Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now