C# 0 The Complete Reference


byte  is needed when assigning  b * b



Download 4,07 Mb.
Pdf ko'rish
bet119/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   115   116   117   118   119   120   121   122   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

byte

 is needed when assigning 



b * b

 back to 



b

! The 


reason is because in 

b * b

, the value of 



b

 is promoted to 



int

 when the expression is evaluated. 

Thus,

b * b 

results in an 



int

 value, which cannot be assigned to a 



byte

 variable without a 

cast. Keep this in mind if you get unexpected type-incompatibility error messages on 

expressions that would otherwise seem perfectly correct.

This same sort of situation also occurs when performing operations on 

char

s. For 


example, in the following fragment, the cast back to 

char

 is needed because of the 

promotion of 

ch1

 and 


ch2

 to 


int

 within the expression

char ch1 = 'a', ch2 = 'b';

ch1 = (char) (ch1 + ch2);

Without the cast, the result of adding 

ch1

 to 


ch2

 would be 



int

, which can’t be assigned to 

a

char

.

Type promotions also occur when a unary operation, such as the unary 



, takes place. 

For the unary operations, operands smaller than 

int

 (

byte

,

sbyte

,

short

, and 

ushort

) are 


promoted to 

int

. Also, a 



char

 operand is converted to 



int

. Furthermore, if a 



uint

 value is 

negated, it is promoted to 

long

.

Using Casts in Expressions

A cast can be applied to a specific portion of a larger expression. This gives you fine-grained 

control over the way type conversions occur when an expression is evaluated. For example, 

consider the following program. It displays the square roots of the numbers from 1 to 10. It 

also displays the whole number portion and the fractional part of each result, separately. To 

do so, it uses a cast to convert the result of 

Math.Sqrt( )

 to 


int

.

// Using casts in an expression.



using System;

class CastExpr {

  static void Main() {

    double n;

     for(n = 1.0; n <= 10; n++) {

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   115   116   117   118   119   120   121   122   ...   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