C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet772/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   768   769   770   771   772   773   774   775   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

The using Statement

In addition to the 



using

directive

 discussed earlier, 



using

 has a second form that is called the 



using

statement.

 It has these general forms:

using (

obj

) {


   // use 

obj

}

using (



type obj

 = 


initializer

) {


  // use 

obj

}

www.freepdf-books.com




608

 

P a r t   I :  



T h e   C #   L a n g u a g e

Here, 


obj

 is an expression that must evaluate to an object that implements the 



System.IDisposable

 interface. It specifies a variable that will be used inside the 



using

block. In the first form, the object is declared outside the 



using

 statement. In the second 

form, the object is declared within the 

using

 statement. When the block concludes, the 



Dispose( )

 method (defined by the 



System

.

IDisposable

 interface) will be called on 

obj

.

Dispose( )

 is called even if the 

using

 block ends because of an exception. Thus, a 



using

statement provides a means by which objects are automatically disposed when they are no 

longer needed. Remember, the 

using

 statement applies only to objects that implement the 



System.IDisposable

interface.

Here is an example of each form of the 

using

 statement:

// Demonstrate using statement.

using System;

using System.IO;

class UsingDemo {

  static void Main() {

    try {

      StreamReader sr = new StreamReader("test.txt");

      // Use object inside using statement.

      using(sr) {

        // ...

      }

    } catch(IOException exc) {

       // ...

    }


    try {

      // Create a StreamReader inside the using statement.

      using(StreamReader sr2 = new StreamReader("test.txt")) {

        // ...

      }

    } catch(IOException exc) {

      // ...

    }


  }

}

The class 



StreamReader

 implements the 



IDisposable

 interface (through its base class 



TextReader

). Thus, it can be used in a 



using

 statement. When the 



using

 statement ends, 



Dispose( )

 is automatically called on the stream variable, thus closing the stream.

As the preceding example illustrates, 

using

 is particularly useful when working with 

files because the file is automatically closed at the end of the 

using

 block, even if the block 

ends because of an exception. As a result, closing a file via 

using

 often simplifies file-

handling code.

extern

The


extern

 keyword has two uses. Each is examined here.

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   768   769   770   771   772   773   774   775   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish