常用代码片段
代码 常用 片段
2023-09-14 09:08:45 时间
实现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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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>
把这段XML文档存储为dispoase.snippet,保存到你的VS下面的某个目录下(我的是(C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC#\Snippets\2052\Visual C#))
重启VS,可见效果如图所示。(这是从Surviveplus Code Snippets发现的一个snippet,非常好用)
相关文章
- Java常用代码_计算机植入木马程序
- 了解几种常用的哈希校验码是什么_代码有哪些校验方式
- 一些常用的标签丨直接代码「建议收藏」
- 10个常用的损失函数解释以及Python代码实现
- IDEA 常用代码模板
- 归档 | 如何使用 Github Gist 来实现代码块高亮自由
- SAP QM 不常用事务代码QVM3 - Inspection Lots Without Usage Decision
- 攻击者可滥用 GitHub 代码空间传递恶意软件
- 全面审查Twitter代码、当场炒掉CEO等众多高管:马斯克正式入主Twitter
- 常用的Java代码汇总详解编程语言
- 常用十六进制颜色对照表代码查询详解编程语言
- 本人常用的分页代码
- AjaxPHP简单入门教程代码
- 常用的javascriptfunction代码
- asp.net下常用的加密算法MD5、SHA-1应用代码
- 一些常用的JS功能函数代码
- 利用onresize使得div可以随着屏幕大小而自适应的代码
- PHP的几个常用数字判断函数代码
- 高仿网易新闻顶部滑动条效果实现代码
- Android模拟器中窗口截图存成文件实现思路及代码
- 使用js检测浏览器的实现代码
- php对图像的各种处理函数代码小结
- jsp使用jstl实现翻页实例代码
- php中操作memcached缓存进行增删改查数据的实现代码