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



Download 21,46 Mb.
Pdf ko'rish
bet212/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   208   209   210   211   212   213   214   215   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Set initial string value.

string s1 = "This is my string.";

Console.WriteLine("s1 = {0}", s1);



// Uppercase s1?

string upperString = s1.ToUpper();

Console.WriteLine("upperString = {0}", upperString);

// Nope! s1 is in the same format!

Console.WriteLine("s1 = {0}", s1);

}

C H A P T E R   3  



C O R E   C #   P R O G R A M M I N G   C O N S T R U C T S, PA RT   I



91

8849CH03.qxd  9/24/07  11:07 AM  Page 91




If you examine the relevant output in Figure 3-12, you can verify that the original string object

(s1) is not uppercased when calling ToUpper(), rather you are returned a copy of the string in a

modified format.

Figure 3-12.

Strings are immutable!

The same law of immutability holds true when you use the C# assignment operator. To illus-

trate, comment out (or delete) any existing code within StringAreImmutable() (to decrease the

amount of generated CIL code) and add the following code statements:

static void StringAreImmutable()

{

string s2 = "My other string";



s2 = "New string value";

}

Now, compile your application and load the assembly into ildasm.exe (again, see Chapter 1). If



you were to double-click the Main() method, you would find the CIL code shown in Figure 3-13.

Figure 3-13.

Assigning a value to a string object results in a new string object.

Although we have yet to examine the low-level details of the Common Intermediate Language

(CIL), do note that the Main() method makes numerous calls to the ldstr (load string) opcode. Sim-

ply put, the ldstr opcode of CIL loads a new string object on the managed heap. The previous

string object that contained the value "My other string." will eventually be garbage collected.

So, what exactly are we to gather from this insight? In a nutshell, the string type can be ineffi-

cient and result in bloated code if misused, especially when performing string concatenation. If you

need to represent basic character data such as a US Social Security number (SSN), first or last

names, or simple string literals used within your application, the string data type is the perfect

choice.


C H A P T E R   3  


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   208   209   210   211   212   213   214   215   ...   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