C# 0 The Complete Reference


PART I C h a p t e r   8



Download 4,07 Mb.
Pdf ko'rish
bet244/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   240   241   242   243   244   245   246   247   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   8 :  

A   C l o s e r   L o o k   a t   M e t h o d s   a n d   C l a s s e s  

175


PART IPART I

    int a = 15, b = 20;

    Console.WriteLine("a and b before call: " +

                       a + " " + b);

    ob.NoChange(a, b);

    Console.WriteLine("a and b after call: " +

                       a + " " + b);

  }


}

The output from this program is shown here:

a and b before call: 15 20

a and b after call: 15 20

As you can see, the operations that occur inside 

NoChange( )

 have no effect on the values of 



a

 and 


b

 used in the call. Again, this is because 



copies

 of the 


value

 of 


a

 and 


b

 have been given 

to parameters 

i

 and 


j

, but 


a

 and 


b

 are otherwise completely independent of 



i

 and 


j

. Thus, 


assigning

i

 a new value will not affect 



a

.

When you pass a reference to a method, the situation is a bit more complicated. In this 



case, the reference, itself, is still passed by value. Thus, a copy of the reference is made and 

changes to the parameter will not affect the argument. (For example, making the parameter 

refer to a new object will not change the object to which the argument refers.) However—

and this is a big however—changes 



made to the object

 being referred to by the parameter 



will

affect the object referred to by the argument. Let’s see why.

Recall that when you create a variable of a class type, you are only creating a reference 

to an object. Thus, when you pass this reference to a method, the parameter that receives it 

will refer to the same object as that referred to by the argument. Therefore, the argument 

and the parameter will both refer to the same object. This means that objects are passed to 

methods by what is effectively call-by-reference. Thus, changes to the object inside the method 

do

 affect the object used as an argument. For example, consider the following program:

// Objects are passed by reference.

using System;

class Test {

  public int a, b;

  public Test(int i, int j) {

    a = i;

    b = j;

  }


  /* Pass an object. Now, ob.a and ob.b in object

     used in the call will be changed. */

  public void Change(Test ob) {

    ob.a = ob.a + ob.b;

    ob.b = -ob.b;

  }


}

www.freepdf-books.com




176

 

P a r t   I :  



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

class CallByRef {

  static void Main() {

    Test ob = new Test(15, 20);

    Console.WriteLine("ob.a and ob.b before call: " +

                       ob.a + " " + ob.b);

    ob.Change(ob);

    Console.WriteLine("ob.a and ob.b after call: " +

                       ob.a + " " + ob.b);

  }


}

This program generates the following output:

ob.a and ob.b before call: 15 20

ob.a and ob.b after call: 35 -20

As you can see, in this case, the actions inside 

Change( )

 have affected the object used as an 

argument.

To review: When a reference is passed to a method, the reference itself is passed by use 

of call-by-value. Thus, a copy of that reference is made. However, the copy of that reference 

will still refer to the same object as its corresponding argument. This means that objects are 

implicitly passed using call-by-reference.


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   240   241   242   243   244   245   246   247   ...   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