Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 9 STATEMENTS

Another Form of the using Statement

Another form of the using statement is the following:

Keyword

Resource

 

Uses resource

 

 

 

 

 

 

 

using ( Expression ) EmbeddedStatement

 

 

In this form, the resource is declared before the using statement.

 

 

 

 

TextWriter tw = File.CreateText("Lincoln.txt");

// Resource declared

 

using ( tw )

 

 

 

// using statement

 

tw.WriteLine("Four score and seven years ago, ...");

 

Although this form still ensures that the Dispose method will always be called after you finish using the resource, it does not protect you from attempting to use the resource after the using statement has released its unmanaged resources, leaving it in an inconsistent state. It therefore gives less protection and is discouraged. This form is illustrated in Figure 9-12.

Figure 9-12. Resource declaration before the using statement

266

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]