Use of "Using" statement for IDisposable

I am reading some Microsoft documentation on XmlReader. The documentation is great, however I am looking for an explanation as to why they choose to implement the
use of a using statement at line 1? I read the documentation on the "using" statement, and it seems to be geared twords when you are using IDisposable:

Does XmlReader implement the IDisposable object? If I had to explain to someone else why a using statement is used at line 1 below, I really could not do it. Perhaps someone can help me to understand.

// Create an XmlReader
1   using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
2 {
3    reader.ReadToFollowing("book");
4    reader.MoveToFirstAttribute();
5    string genre = reader.Value;
6    output.AppendLine("The genre value: " + genre);

 7   reader.ReadToFollowing("title");
 8   output.AppendLine("Content of the title element: " + reader.ReadElementContentAsString());
Who is Participating?

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

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.

ste5anSenior DeveloperCommented:
That's why we have F12. Locate the cursor on the XmlReader in the declaration and press F12.

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
Russ SuterSenior Software DeveloperCommented:
A few points.

1. The using clause is a terrific tool!
2. You cannot use a using clause on an object that does not implement iDisposable. The compiler just won't let you.
3. Yes, XmlReader does implement iDisposable.
4. Now for the longer explanation...

Most classes in .NET can just live happily in the CLR and let the built-in memory manager do its job but classes that use unmanaged resources, like streams for example, need a clean way to dispose. This is where the iDisposable object comes in. You can read more about iDisposable in this well written article.

In a nutshell, if you create an instance of an object that uses unmanaged resources you should call class.Dispose() when you're finished. This tells the CLR that the resources are no longer needed and can be cleaned up by the garbage collector. You might be surprised to find out how much code there is out there that creates instances of such classes but fails to dispose of them.

The Using clause restricts the classes such that as soon as they go out of scope the Dispose() method is automatically called. This makes for cleaner code, less chance of programmer error, and better memory management and utilization by your application.

Short answer: If a class implements iDisposable, use "using". If it doesn't, you just can't.
brgdotnetcontractorAuthor Commented:
Russ you Rock. Thanks
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

From novice to tech pro — start learning today.