1

I've been browsing the .NET Framework source code this morning as I just wanted to check the behaviour of Dipose() on database connections.

However, viewing the .NET Framework source for SqlConnection (+ I've downloaded it as well), there is no method Dispose() defined in SqlConnection.

However, using dotPeek, I can see an implementation of Dispose() on SqlConnection

What am I missing?

3
  • Where do you see the SqlConnection that does nothing in the Dispose method? referencesource.microsoft.com/#System.Data/System/Data/… Commented Dec 26, 2014 at 10:40
  • Hard to guess what you are looking at, but it can't be the Dispose() method. The class doesn't re-implement IDisposable, it relies on the disposable pattern inherited from its base class. Look here instead. Do keep in mind that the Reference Source is not complete for System.Data, parts of it were written in C++/CLI which is not included. Commented Dec 26, 2014 at 10:41
  • @Steve Where do you see a definition of Dispose() in that file? It just contains a DisposeMe() helper method, but not the Dispose() method. (It does contain SqlDebugContext.Dispose(), but not SqlConnection.Dispose().) Commented Dec 26, 2014 at 10:45

3 Answers 3

5

All you're really missing is that SqlConnection.cs defines SqlConnection as a partial class. This means other files can define member functions, including Dispose(). It's one of those other files that is defining the Dispose() member function.

Take a look at DbConnectionHelper.cs. This defines a class CONNECTIONOBJECTNAME in namespace NAMESPACE. Those would be replaced at build-time by the correct class name and namespace name, and this allows multiple DbConnection-derived classes to share the Dispose() implementation.

1
  • Thanks. I spotted it was partial but as couldn't find another definition, assumed there wasn't. Hadn't considered the option of them being generated at build time. Commented Dec 26, 2014 at 10:56
0

Seems like the Dispose() method doesn't have a specific implementation in SqlConnection, and just inherits from Component.

You can check the SqlConnection documentation which confirms that.

-1

Only classes that implements IDisposable interface can have the Dispose (Unless you write yourown) method.

SqlConnection class has the Dispose method because it's inherit's Component

Source: here

And you can find more about IDisposable interface here

Your Answer

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.