0

I have a connection class that is disposable

public class Connection : IDisposable { ... }

and a class that has a dependency on it

public class UsesConnection : IDisposable {
 UsesConnection (Connection c) {...}
 public void Dispose() {
 c.Dispose();
 ...
 }
 ...
}

How do I create and return a UsesConnection class without getting a CA2000 error?

The following code gets a CA2000 error on the line c = new Connection();

Connection c = null;
UsesConnection u = null;
try {
 c = new Connection()
 u = new UsesConnection(c);
 return u;
} catch {
 if (u != null) u.Dispose();
 else if (c != null) c.Dispose();
 throw;
}
asked Jul 25, 2014 at 13:07
2
  • 1
    Do you really want Connection is created outside UsesConnection but disposed together with it? I'd keep ownership well defined. A creates an object and it has responsibility to dispose it. Commented Jul 25, 2014 at 13:14
  • I would suggest to read the MSDN documentation for CA2000, section "When to Suppress Warnings": "Do not suppress a warning from this rule unless you have called a method on your object that calls Dispose, such as Close, or if the method that raised the warning returns an IDisposable object wraps your object." Your case is explicitly mentioned as one where you could ignore/suppress that warning... Commented Jul 25, 2014 at 13:18

1 Answer 1

2

Use a using block instead.

using (var c = new Connection())
using (var u = new UsesConnection(c))
{
 // Do your work here
}

But don't return as that will dispose both objects. Don't let UsesConnection to dispose Connection.

You should be in charge of maintaining the life-cycles of your disposable references, not some caller - ask yourself "what if they never dispose of my object?"

answered Jul 25, 2014 at 13:11
Sign up to request clarification or add additional context in comments.

4 Comments

But he is returning u so this would not work I think?
After looking at it more I agree with this. UsesConnection doesn't "own" the Connection instance passed into it, so it shouldn't do any disposal of it. Suppression may still be the correct answer to getting Code Analysis to accept this, but not for the reasons I stated in my previous answer.
After thinking through the implications of what you have said it seems that you have suggested one of 2 things: 1) create a connection every time that I need it and dispose afterwards (expensive), or 2) create a connection 'higher up' the call stack and have the connection longer lived. Which one would you recommend?
i'd keep it around as long as you need it. higher up in the stack might make the most sense.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.