C# 0 The Complete Reference


obj , which is an  object



Download 4,07 Mb.
Pdf ko'rish
bet403/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   399   400   401   402   403   404   405   406   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

obj

, which is an 



object

 reference. The integer value in 



obj

 is retrieved by casting 



obj

to

int

.

Here is another, more interesting example of boxing. In this case, an 



int

 is passed as an 

argument to the 

Sqr( )

 method, which uses an 



object

 parameter.

// Boxing also occurs when passing values.

using System;

class BoxingDemo {

  static void Main() {

    int x;

    x = 10;

    Console.WriteLine("Here is x: " + x);

    // x is automatically boxed when passed to Sqr().

    x = BoxingDemo.Sqr(x);

    Console.WriteLine("Here is x squared: " + x);

  }

  static int Sqr(object o) {



    return (int)o * (int)o;

  }


}

The output from the program is shown here:

Here is x: 10

Here is x squared: 100

Here, the value of 

x

 is automatically boxed when it is passed to 



Sqr( )

.

Boxing and unboxing allow C#’s type system to be fully unified. All types derive from 



object

. A reference to any type can be assigned to a variable of type 



object

. Boxing and 

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   399   400   401   402   403   404   405   406   ...   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