Created
November 6, 2013 03:00
-
-
Save traktraktrugui/7330187 to your computer and use it in GitHub Desktop.
Code Snippets for the Dispose Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<Title>Base class disposable implementation.</Title> | |
<Shortcut>bdisp</Shortcut> | |
<Description> | |
Code snippet to create a scheleton implementation | |
of the IDisposable pattern for a base class. | |
</Description> | |
<Author>Stefano Ricciardi</Author> | |
<SnippetTypes> | |
<SnippetType>Expansion</SnippetType> | |
</SnippetTypes> | |
</Header> | |
<Snippet> | |
<Declarations> | |
<Literal> | |
<ID>ClassName</ID> | |
<ToolTip>Name of the class.</ToolTip> | |
<Default>ClassName</Default> | |
</Literal> | |
</Declarations> | |
<Code Language="csharp"> | |
<![CDATA[ | |
public class $ClassName$: IDisposable | |
{ | |
private bool _disposed = false; | |
//Implement IDisposable. | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (!_disposed) | |
{ | |
if (disposing) | |
{ | |
// Free other state (managed objects). | |
} | |
// Free your own state (unmanaged objects). | |
// Set large fields to null. | |
_disposed = true; | |
} | |
} | |
// Use C# destructor syntax for finalization code. | |
~$ClassName$() | |
{ | |
// Simply call Dispose(false). | |
Dispose(false); | |
} | |
} ]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<Title>Base class disposable implementation.</Title> | |
<Shortcut>ddisp</Shortcut> | |
<Description> | |
Code snippet to create a scheleton implementation of the | |
IDisposable pattern for a derived class. | |
</Description> | |
<Author>Stefano Ricciardi</Author> | |
<SnippetTypes> | |
<SnippetType>Expansion</SnippetType> | |
</SnippetTypes> | |
</Header> | |
<Snippet> | |
<Declarations> | |
<Literal> | |
<ID>DerivedClassName</ID> | |
<ToolTip>Name of the derived class.</ToolTip> | |
<Default>Derived</Default> | |
</Literal> | |
<Literal> | |
<ID>BaseClassName</ID> | |
<ToolTip>Name of the base class.</ToolTip> | |
<Default>Base</Default> | |
</Literal> | |
</Declarations> | |
<Code Language="csharp"> | |
<![CDATA[ | |
public class $DerivedClassName$: $BaseClassName$ | |
{ | |
private bool _disposed = false; | |
protected override void Dispose(bool disposing) | |
{ | |
if (!_disposed) | |
{ | |
if (disposing) | |
{ | |
// Release managed resources. | |
} | |
// Release unmanaged resources. | |
// Set large fields to null. | |
// Call Dispose on your base class. | |
_disposed = true; | |
} | |
base.Dispose(disposing); | |
} | |
// The derived class does not have a Finalize method | |
// or a Dispose method without parameters because it inherits | |
// them from the base class. | |
} | |
]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\1033\Visual C#\bdisp.snippet" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment