zoukankan      html  css  js  c++  java
  • 常用代码片段

    实现IDisposable的代码片段

     1        ~DemoType()
     2         {
     3             this.Dispose();
     4         }
     5 
     6         #region IDisposable Members
     7 
     8         /// <summary>
     9         /// Internal variable which checks if Dispose has already been called
    10         /// </summary>
    11         protected Boolean disposed;
    12 
    13         /// <summary>
    14         /// Releases unmanaged and - optionally - managed resources
    15         /// </summary>
    16         /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
    17         protected void Dispose(Boolean disposing)
    18         {
    19             if (disposed)
    20             {
    21                 return;
    22             }
    23 
    24             if (disposing)
    25             {
    26                 //TODO: Managed cleanup code here, while managed refs still valid
    27             }
    28             //TODO: Unmanaged cleanup code here
    29 
    30             disposed = true;
    31         }
    32 
    33         /// <summary>
    34         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    35         /// </summary>
    36         public void Dispose()
    37         {
    38             // Call the private Dispose(bool) helper and indicate
    39             // that we are explicitly disposing
    40             this.Dispose(true);
    41 
    42             // Tell the garbage collector that the object doesn't require any
    43             // cleanup when collected since Dispose was called explicitly.
    44             GC.SuppressFinalize(this);
    45         }
    46 
    47         #endregion

    CodeSnippet

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
     3     <CodeSnippet Format="1.0.0">
     4         <Header>
     5       <Title>Implement IDisposable</Title>
     6       <Author>Surviveplus.net</Author>
     7       <Description>Define logic of IDisposable implementation.</Description>
     8       <Shortcut>dispoase</Shortcut>
     9       <SnippetTypes>
    10                 <SnippetType>Expansion</SnippetType>
    11             </SnippetTypes>
    12         </Header>
    13         <Snippet>
    14             <Declarations>
    15                 <Literal Editable="false">
    16                     <ID>classname</ID>
    17                     <ToolTip>Class Name</ToolTip>
    18                     <Function>ClassName()</Function>
    19                     <Default>ClassNamePlaceholder</Default>
    20                 </Literal>
    21             </Declarations>
    22             <Code Language="csharp">
    23         <![CDATA[
    24         #region IDisposable Members
    25 
    26         /// <summary>
    27         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    28         /// </summary>
    29         public void Dispose() {
    30             this.Dispose( true );
    31             GC.SuppressFinalize( this );
    32         } // end sub
    33 
    34         /// <summary>
    35         /// Destruct instance of the class.
    36         /// </summary>
    37         ~$classname$() {
    38             this.Dispose( false );
    39         }
    40 
    41         /// <summary>
    42         /// Backing field to track whether Dispose has been called.
    43         /// </summary>
    44         private bool disposedValue = false;
    45 
    46         /// <summary>
    47         /// Dispose managed and unmanaged resources of this instance.
    48         /// </summary>
    49         /// <param name="disposing">If disposing equals true, managed and unmanaged resources can be disposed. If disposing equals false, only unmanaged resources can be disposed. </param>
    50         protected virtual void Dispose( bool disposing ) {
    51 
    52             if ( this.disposedValue == false ) {
    53                 if ( disposing ) {
    54                     // TODO: Dispose managed resources.
    55                 } // end if
    56 
    57                 // TODO: Dispose unmanaged resources.
    58             } // end if
    59 
    60             this.disposedValue = true;
    61         } // end sub
    62 
    63         #endregion]]>
    64             </Code>
    65         </Snippet>
    66     </CodeSnippet>
    67 </CodeSnippets>
    dispoase.snippet

    把这段XML文档存储为dispoase.snippet,保存到你的VS下面的某个目录下(我的是(C:Program Files (x86)Microsoft Visual Studio 12.0VC#Snippets2052Visual C#))

    重启VS,可见效果如图所示。(这是从Surviveplus Code Snippets发现的一个snippet,非常好用)

  • 相关阅读:
    CSS3中的opacity透明度属性的继承问题如何解决
    webstorm前端开发工具vue环境配置及运行项目
    new String(getBytes(ISO-8859-1),UTF-8)中文编码避免乱码
    超详细多线程讲解
    jQuery mobile 核心功能
    解读四大移动web应用开发框架真相
    2014,成为更好程序员的7个方法
    window8.1使用之快捷键
    C#深入浅出 关键字(一)
    C#深入浅出 C#语法中的重中之重——委托(四)
  • 原文地址:https://www.cnblogs.com/bitzhuwei/p/useful-code-snippets.html
Copyright © 2011-2022 走看看