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


Narrowing and Widening Data Type Conversions



Download 21,46 Mb.
Pdf ko'rish
bet217/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   213   214   215   216   217   218   219   220   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

Narrowing and Widening Data Type Conversions

Now that you understand how to interact with intrinsic data types, let’s examine the related topic of



data type conversion. Assume you have a new Console Application project (named TypeConversions)

that defines the following class type:

class Program

{

static void Main(string[] args)



{

Console.WriteLine("***** Fun with type conversions *****");



// Add two shorts and print the result.

short numb1 = 9, numb2 = 10;

Console.WriteLine("{0} + {1} = {2}",

numb1, numb2, Add(numb1, numb2));

Console.ReadLine();

}

static int Add(int x, int y)



{ return x + y; }

}

Notice that the Add() method expects to be sent two int parameters. However, the Main()



method is in fact sending in two short variables. While this might seem like a complete and total

mismatch of data types, the program compiles and executes without error, returning the expected

result of 19.

The reason that the compiler treats this code as syntactically sound is due to the fact that there

is no possibility for loss of data. Given that the maximum value of a short (32,767) is well within the

range of an int (2,147,483,647), the compiler implicitly widens each short to an int. Formally

speaking, 

widening is the term used to define an implicit “upward cast” that does not result in a

loss of data.




Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   213   214   215   216   217   218   219   220   ...   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