C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet133/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   129   130   131   132   133   134   135   136   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

The Assignment Operator

The


assignment operator

 is the single equal sign, 



=

. The assignment operator works in C# 

much as it does in other computer languages. It has this general form:

var-name

=

 expression

;

Here, the type of 



var-name

 must be compatible with the type of 



expression.

www.freepdf-books.com




72

 

P a r t   I :  



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

The assignment operator does have one interesting attribute that you may not be 

familiar with: It allows you to create a chain of assignments. For example, consider this 

fragment:

int x, y, z;

x = y = z = 100; // set x, y, and z to 100

This fragment sets the variables 

x

,

y

, and 

z

 to 100 using a single statement. This works 

because the 

=

 is an operator that yields the assigned value. Thus, the value of 



z = 100

 is 100, 

which is then assigned to 

y

, which in turn is assigned to 



x

. Using a “chain of assignment” is 

an easy way to set a group of variables to a common value.

Compound Assignments

C# provides special compound assignment operators that simplify the coding of certain 

assignment statements. Let’s begin with an example. The assignment statement shown here:

x = x + 10;

can be written using a compound assignment as

x += 10;


The operator pair 

+=

 tells the compiler to assign to 



x

 the value of 



x

 plus 10.

Here is another example. The statement

x = x - 100;

is the same as

x -= 100;

Both statements assign to 

x

 the value of 



x

 minus 100.

There are compound assignment operators for many of the binary operators (that is, 

those that require two operands). The general form of the shorthand is



var-name op

 = 


expression

;

Thus, the arithmetic and logical assignment operators are



+=

–=

*=



/=

%=

&=



|=

^=

Because the compound assignment statements are shorter than their noncompound 



equivalents, the compound assignment operators are also sometimes called the 

shorthand

assignment

 operators.

The compound assignment operators provide two benefits. First, they are more compact 

than their “longhand” equivalents. Second, they can result in more efficient executable code 

(because the left-hand operand is evaluated only once). For these reasons, you will often see 

the compound assignment operators used in professionally written C# programs.

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   129   130   131   132   133   134   135   136   ...   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