posts - 78, comments - 79, trackbacks - 84

My Links

News

View Anthony Trudeau's profile on LinkedIn

Add to Technorati Favorites

Article Categories

Archives

Post Categories

Other Links

Disposing pattern code snippet

I had some time today when my wife was at the mall to play around with the code snippets finally.  I think I've really missed out on something by not getting into them sooner.  But, hey, I'm like a kid with a new toy.  My first creation is the disposing pattern that I use frequently.

<?xml version="1.0" encoding="utf-8"?>
<
CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
     <
CodeSnippet Format="1.0.0">
          <
Header><Title> Disposing pattern </Title>
               <
Description>Implement the disposing pattern with the corresponding finalizer (destructor).</Description>
               <
Keywords>
                   <
Keyword>Dispose</Keyword>
                   <
Keyword>Finalizer</Keyword>
                   <
Keyword>Destructor</Keyword>
              </
Keywords>
              <
Author>Anthony Trudeau</Author>
         </
Header>
         <
Snippet>
         <
Declarations>
              <
Literal Editable="true">
                  <
ID>ClassName</ID>
                  <
Function>ClassName()</Function>
             </
Literal>
        </
Declarations>
        <
Code Language="CSharp">
            <![CDATA[
~$ClassName$()
{
     Dispose(false);
}

public void Dispose()
{
     Dispose(true);
     GC.SuppressFinalize(this);
}

private bool _disposed; // indicates if Dispose has been called

private void Dispose(bool disposing)
{
     if (!_disposed)
    {
          if (disposing)
         {
              // TODO: cleanup managed resources in $ClassName$
         }

         // TODO: cleanup unmanaged resources in $ClassName$
        _disposed = true;
     }
            }
]]>
        </
Code>
       </
Snippet>
    </
CodeSnippet>
</
CodeSnippets>

 

Print | posted on Saturday, April 15, 2006 3:05 PM | Filed Under [ .NET ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 5 and type the answer here:

Powered by: