IDisposable 接口
提供一种用于释放非托管资源的机制。
地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.idisposable?view=netframework-4.8
标题:IDisposable 接口
using System;
using System.IO;
using System.Text.RegularExpressions; public class WordCount
{
private String filename = String.Empty;
private int nWords = ;
private String pattern = @"\b\w+\b"; public WordCount(string filename)
{
if (! File.Exists(filename))
throw new FileNotFoundException("The file does not exist."); this.filename = filename;
string txt = String.Empty;
using (StreamReader sr = new StreamReader(filename)) {
txt = sr.ReadToEnd();
}
nWords = Regex.Matches(txt, pattern).Count;
} public string FullName
{ get { return filename; } } public string Name
{ get { return Path.GetFileName(filename); } } public int Count
{ get { return nWords; } }
}
using语句实际上是语法上的便利。 在编译时, 语言编译器将为try / finally块实现中间语言 (IL)。
有关using语句的详细信息, 请参阅using 语句或using 语句主题。
Try/Finally 块
如果编程语言不支持像或using Visual Basic 中C#的语句这样的构造, 或者不想使用它, IDisposable.Dispose则可以从finally try /语句。 finally 下面的示例将上using一示例try / finally中的块替换为块。
using System;
using System.IO;
using System.Text.RegularExpressions; public class WordCount
{
private String filename = String.Empty;
private int nWords = ;
private String pattern = @"\b\w+\b"; public WordCount(string filename)
{
if (! File.Exists(filename))
throw new FileNotFoundException("The file does not exist."); this.filename = filename;
string txt = String.Empty;
StreamReader sr = null;
try {
sr = new StreamReader(filename);
txt = sr.ReadToEnd();
}
finally {
if (sr != null) sr.Dispose();
}
nWords = Regex.Matches(txt, pattern).Count;
} public string FullName
{ get { return filename; } } public string Name
{ get { return Path.GetFileName(filename); } } public int Count
{ get { return nWords; } }
}
IDisposable 接口的更多相关文章
- C#中对IDisposable接口的理解
http://blog.sina.com.cn/s/blog_8abeac5b01019u19.html C#中对IDisposable接口的理解 本人最近接触一个项目,在这个项目里面看到很多类实现了 ...
- IDisposable接口
C#中IDisposable接口的主要用途是释放非托管资源.当不再使用托管对象时,垃圾回收器会自动释放分配给该对象的内存.但无法预测进行垃圾回收的时间.另外,垃圾回收器对窗口句柄或打开的文件和流等非托 ...
- IDisposable 接口2
定义一种释放分配的资源的方法. 命名空间: System程序集: mscorlib(在 mscorlib.dll 中) 语法 C# C++ F# VB [ComVisibleAttribute(t ...
- IDisposable接口详解
转载:http://www.cnblogs.com/davyli/archive/2010/09/13/1825042.html 正确实现 IDisposable .NET中用于释放对象资源的接口是I ...
- .NET中IDisposable接口的基本使用
首先来看MSDN中关于这个接口的说明: [ComVisible(true)] public interface IDisposable { // Methods void Dispose(); } 1 ...
- 利用IDisposable接口构建包含非托管资源对象
托管资源与非托管资源 在.net中,对象使用的资源分为两种:托管资源与非托管资源.托管资源由CLR进行管理,不需要开发人员去人工进行控制,.NET中托管资源主要指"对象在堆中的内存" ...
- 深入理解C#中的IDisposable接口
写在前面 在开始之前,我们需要明确什么是C#(或者说.NET)中的资源,打码的时候我们经常说释放资源,那么到底什么是资源,简单来讲,C#中的每一种类型都是一种资源,而资源又分为托管资源和非托管资源,那 ...
- C#中的IDisposable接口
深入理解C#中的IDisposable接口 写在前面 在开始之前,我们需要明确什么是C#(或者说.NET)中的资源,打码的时候我们经常说释放资源,那么到底什么是资源,简单来讲,C#中的每一种类型都是一 ...
- 【转】C#中对IDisposable接口的理解
IDisposable接口定义:定义一种释放分配的资源的方法. .NET 平台在内存管理方面提供了GC(Garbage Collection),负责自动释放托管资源和内存回收的工作,但它无法对非托管资 ...
- 【转】C# 的 IDisposable 接口
C# 的 IDisposable 接口 我在微软的团队快被微软 C# 里面的各种 IDisposable 对象给折腾疯了…… 故事比较长,先来科普一下.如果你没有用过 C#,IDisposable 是 ...
随机推荐
- Android studio中怎么导入android.support.v4包
Android studio中怎么导入android.support.v4包 1.File点击选择projectStructure选择对应的APP然后点击Dependencies 2.点击+号,点击第 ...
- JS中 (function(){...})()立即执行函数
(function(){...})() (function(){...}()) 这是两种js立即执行函数的常见写法. 基本概念: 函数声明:function fname(){...}; 使用funct ...
- GitHub for mobile 来了,码农苦逼了!
北京时间 2019 年 11 月 14 日 GitHub Universe 2019 大会上,GitHub 正式发布了 GitHub for mobile,即 GitHub 的移动版本,支持 iOS ...
- js追加html元素
jquery追加html代码,添加元素 .append() //新增仲裁申请人 $("."+inputName).append("<div class=\" ...
- 运维-nginx +php 错误状态码说明
常用 nginx 错误码: 正常: 200 正常访问 301 永久跳转访问 302 临时跳转访问 常见错误状态码: 400 ,经常有服务器自己调用 自己的情况 用报400,如高防调用高防,或者lv ...
- Spring Security教程之整合SpringMVC(六)
一.前言 Spring Security系列教程中,前五篇为同一人所写,而本文是博主依据第三方文章整合而出,与前五篇文章的作者不是同一系列. 但本文以前五篇文章为基础,在前面文章所建立的Spring ...
- 使用IDEA创建一个Servlet应用程序
使用IDEA创建一个Servlet应用程序 第一步:创建web应用 选择web application应用,之后填写项目名称等. 第二步:项目配置 在WEB-INF目录下创建两个文件夹:classes ...
- Asp.Net Core 2.x 和 3.x WebAPI 使用 Swagger 时 API Controller 控制器 Action 方法 隐藏 hidden 与 and 分组 group
1.前言 为什么我们要隐藏部分接口? 因为我们在用swagger代替接口的时候,难免有些接口会直观的暴露出来,比如我们结合Consul一起使用的时候,会将健康检查接口以及报警通知接口暴露出来,这些接口 ...
- jQuery必知必会
原文地址:https://my.oschina.net/u/218421/blog/37391 jQuery优势 轻量级 强大的选择器 出色的DOM操作的封装 可靠的事件处理机制 完 ...
- QuantLib 金融计算——案例之普通欧式期权分析
目录 QuantLib 金融计算--案例之普通欧式期权分析 概述 普通欧式期权公式法定价 1. 配置期权合约条款 2. 构建期权对象 3. 配置定价引擎 4. 计算 题外话:天数计算规则 Quote ...