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


Setting Object References to null



Download 21,46 Mb.
Pdf ko'rish
bet506/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   502   503   504   505   506   507   508   509   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

Setting Object References to null

Those of you who have created COM objects using Visual Basic 6.0 were well aware that it was

always preferable to set their references to Nothing when you were finished using them. Under the

covers, the reference count of the COM object was decremented by one, and may be removed from

memory if the object’s reference count equaled 0. In a similar fashion, C/C++ programmers often

set pointer variables to null to ensure they are no longer referencing unmanaged memory.

C H A P T E R   8  

U N D E R S TA N D I N G   O B J E C T   L I F E T I M E



248

8849CH08.qxd  10/22/07  1:27 PM  Page 248




Given these facts, you might wonder what the end result is of assigning object references to

null under C#. For example, assume the MakeACar() subroutine has now been updated as follows:

static void MakeACar()

{

Car myCar = new Car();



myCar = null;

}

When you assign object references to null, the compiler will generate CIL code that ensures



the reference (myCar in this example) no longer points to any object. If you were once again to make

use of ildasm.exe to view the CIL code of the modified MakeACar(), you would find the ldnull

opcode (which pushes a null value on the virtual execution stack) followed by a stloc.0 opcode

(which sets the null reference on the allocated Car):

.method private hidebysig static void MakeACar() cil managed

{

// Code size       10 (0xa)



.maxstack  1

.locals init ([0] class SimpleGC.Car myCar)

IL_0000:  nop

IL_0001:  newobj instance void SimpleGC.Car::.ctor()

IL_0006:  stloc.0


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   502   503   504   505   506   507   508   509   ...   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