This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy


// Use a comma-delimited list to declare multiple objects to dispose



Download 21,46 Mb.
Pdf ko'rish
bet536/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   532   533   534   535   536   537   538   539   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Use a comma-delimited list to declare multiple objects to dispose.

using(MyResourceWrapper rw = new MyResourceWrapper(),

rw2 = new MyResourceWrapper())

{

// Use rw and rw2 objects.



}

}



Source Code

The SimpleDispose project is included under the Chapter 8 subdirectory.



Building Finalizable and Disposable Types

At this point, we have seen two different approaches to construct a class that cleans up internal

unmanaged resources. On the one hand, we could override System.Object.Finalize(). Using this

technique, we have the peace of mind that comes with knowing the object cleans itself up when

garbage collected (whenever that may be) without the need for user interaction. On the other hand,

we could implement IDisposable to provide a way for the object user to clean up the object as soon

as it is finished. However, if the caller forgets to call Dispose(), the unmanaged resources may be

held in memory indefinitely.

As you might suspect, it is possible to blend both techniques into a single class definition. By

doing so, you gain the best of both models. If the object user does remember to call Dispose(), you

can inform the garbage collector to bypass the finalization process by calling GC.SuppressFinalize().

If the object user 



forgets to call Dispose(), the object will eventually be finalized and have a chance

to free up the internal resources. The good news is that the object’s internal unmanaged resources

will be freed one way or another.

Here is the next iteration of MyResourceWrapper, which is now finalizable and disposable,

defined in a C# Console Application named FinalizableDisposableClass:

C H A P T E R   8  




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   532   533   534   535   536   537   538   539   ...   1830




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