C# 0 The Complete Reference


PART I C h a p t e r   3



Download 4,07 Mb.
Pdf ko'rish
bet113/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   109   110   111   112   113   114   115   116   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   3 :  

D a t a   T y p e s ,   L i t e r a l s ,   a n d   V a r i a b l e s  

57

PART IPART I



example, if the value 1.23 is assigned to an integer, the resulting value will simply be 1. 

The 0.23 is lost.

The following program demonstrates some type conversions that require casts. It also 

shows some situations in which the casts cause data to be lost.

// Demonstrate casting.

using System;

class CastDemo {

  static void Main() {

    double x, y;

    byte b;

    int i;

    char ch;

    uint u;

    short s;

    long l;

    x = 10.0;

    y = 3.0;

    // Cast double to int, fractional component lost.

    i = (int) (x / y);

    Console.WriteLine("Integer outcome of x / y: " + i);

    Console.WriteLine();

    // Cast an int into a byte, no data lost.

    i = 255;

    b = (byte) i;

    Console.WriteLine("b after assigning 255: " + b +

                      " -- no data lost.");

    // Cast an int into a byte, data lost.

    i = 257;

    b = (byte) i;

    Console.WriteLine("b after assigning 257: " + b +

                      " -- data lost.");

    Console.WriteLine();

    // Cast a uint into a short, no data lost.

    u = 32000;

    s = (short) u;

    Console.WriteLine("s after assigning 32000: " + s +

                      " -- no data lost.");

    // Cast a uint into a short, data lost.

    u = 64000;

    s = (short) u;

    Console.WriteLine("s after assigning 64000: " + s +

                      " -- data lost.");

    Console.WriteLine();

www.freepdf-books.com




58

 

P a r t   I :  



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

    // Cast a long into a uint, no data lost.

    l = 64000;

    u = (uint) l;

    Console.WriteLine("u after assigning 64000: " + u +

                      " -- no data lost.");

    // Cast a long into a uint, data lost.

    l = -12;

    u = (uint) l;

    Console.WriteLine("u after assigning -12: " + u +

                      " -- data lost.");

    Console.WriteLine();

    // Cast an int into a char.

    b = 88; // ASCII code for X

    ch = (char) b;

    Console.WriteLine("ch after assigning 88: " + ch);

  }

}

The output from the program is shown here:



Integer outcome of x / y: 3

b after assigning 255: 255 -- no data lost.

b after assigning 257: 1 -- data lost.

s after assigning 32000: 32000 -- no data lost.

s after assigning 64000: -1536 -- data lost.

u after assigning 64000: 64000 -- no data lost.

u after assigning -12: 4294967284 -- data lost.

ch after assigning 88: X

Let’s look at each assignment. The cast of 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   109   110   111   112   113   114   115   116   ...   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