`
talin2010
  • 浏览: 503232 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

15.13 The using statement

 
阅读更多
The using statement obtains one or more resources, executes a statement, and
then disposes of the resource.
using-statement:
using ( resource-acquisition ) embedded-statement
resource-acquisition:
local-variable-declaration
expression
A resource is a class or struct that implements System.IDisposable, which
includes a single
parameterless method named Dispose. Code that is using a resource can call
Dispose to indicate that the
resource is no longer needed. If Dispose is not called, then automatic
disposal eventually occurs as a
consequence of garbage collection.
If the form of resource-acquisition is local-variable-declaration then the
type of the local-variabledeclaration
must be System.IDisposable or a type that can be implicitly converted to
System.IDisposable. If the form of resource-acquisition is expression then
this expression must be
System.IDisposable or a type that can be implicitly converted to
System.IDisposable.
Chapter 15 Statements
199
Local variables declared in a resource-acquisition are read-only, and must
include an initializer. A compiletime
error occurs if the embedded statement attempts to modify these local
variables (via assignment or the
++ and -- operators) or pass them as ref or out parameters.
A using statement is translated into three parts: acquisition, usage, and
disposal. Usage of the resource is
implicitly enclosed in a try statement that includes a finally clause. This
finally clause disposes of the
resource. If a null resource is acquired, then no call to Dispose is made,
and no exception is thrown.
A using statement of the form
using (ResourceType resource = expression) statement
corresponds to one of two possible expansions. When ResourceType is a value
type, the expansion is
{
ResourceType resource = expression;
try {
statement;
}
finally {
((IDisposable)resource).Dispose();
}
}
Otherwise, when ResourceType is a reference type, the expansion is
{
ResourceType resource = expression;
try {
statement;
}
finally {
if (resource != null) ((IDisposable)resource).Dispose();
}
}
In either expansion, the resource variable is read-only in the embedded
statement.
A using statement of the form
using (expression) statement
has the same two possible expansions, but in this case ResourceType is
implicitly the compile-time type of
the expression, and the resource variable is inaccessible in, and invisible
to, the embedded statement.
When a resource-acquisition takes the form of a local-variable-declaration,
it is possible to acquire multiple
resources of a given type. A using statement of the form
using (ResourceType r1 = e1, r2 = e2, ..., rN = eN) statement
is precisely equivalent to a sequence of nested using statements:
using (ResourceType r1 = e1)
using (ResourceType r2 = e2)
...
using (ResourceType rN = eN)
statement
[Example: The example below creates a file named log.txt and writes two
lines of text to the file. The
example then opens that same file for reading and copies the contained
lines of text to the console.
using System;
using System.IO;
class Test
{
static void Main() {
using (TextWriter w = File.CreateText("log.txt")) {
w.WriteLine("This is line one");
w.WriteLine("This is line two");
}
C# LANGUAGE SPECIFICATION
200
using (TextReader r = File.OpenText("log.txt")) {
string s;
while ((s = r.ReadLine()) != null) {
Console.WriteLine(s);
}
}
}
}
Since the TextWriter and TextReader classes implement the IDisposable
interface, the example can
use using statements to ensure that the underlying file is properly closed
following the write or read
operations. end example]
分享到:
评论

相关推荐

    Categorical Data Analysis Using The SAS System

    8.9 Using the CATMOD and GENMOD Procedures for Logistic Regression . . . 232 Appendix A: Statistical Methodology for Dichotomous Logistic Regression . . . 239 Chapter 9. Logistic Regression II: ...

    腾达固件A18V1.0RTL_V15.13.07.09

    腾达固件A18V1.0RTL_V15.13.07.09官方固件腾达固件A18V1.0RTL_V15.13.07.09官方固件

    C#开发之——控件的应用案例(15.13) c#经典案例.pdf

    C#开发之——控件的应用案例(15.13) c#经典案例.pdf

    计量经济学及stata应用 15.如何做实证研究 15.13提交论文或投稿.mp4

    计量经济学及stata应用 15.如何做实证研究 15.13提交论文或投稿.mp4

    node-v15.13.0-x86.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    teamviewer_15.13.6_amd64.deb

    ubuntu安装包

    node-v15.13.0-x64.msi

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-win-x86.zip

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-win-x64.zip

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-darwin-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-linux-s390x.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-linux-arm64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-linux-x64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-darwin-x64.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-linux-x64.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-linux-arm64.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-linux-s390x.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

    node-v15.13.0-aix-ppc64.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 ...

Global site tag (gtag.js) - Google Analytics