C# 0 The Complete Reference


SameAs( )  and  Copy( )



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

SameAs( )

 and 


Copy( )

 methods each take a reference of type 



MyClass

 as an 


argument. The 

SameAs( )

 method compares the values of 



alpha

 and 


beta

 in the invoking 

object with the values of 

alpha

 and 


beta

 in the object passed via 



ob

. The method returns 



true

 only if the two objects contain the same values for these instance variables. The 



Copy( )

method assigns the values of 



alpha

 and 


beta

 in the object referred to by 



ob

 to 


alpha

 and 


beta

 in the invoking object. As this example shows, syntactically, reference types are passed 

to methods in the same way as are value types.

How Arguments Are Passed

As the preceding example demonstrated, passing an object reference to a method is a 

straightforward task. However, there are some nuances that the example did not show. 

In certain cases, the effects of passing a reference type will be different than those experienced 

when passing a value type. To see why, let’s review the two ways in which an argument 

can be passed to a subroutine.

The first way is 

call-by-value.

 This method 



copies

 the 


value

 of an argument into the formal 

parameter of the subroutine. Therefore, changes made to the parameter of the subroutine 

have no effect on the argument used in the call. The second way an argument can be passed 

is

call-by-reference.

 In this method, a 



reference

 to an argument (not the value of the argument) 

is passed to the parameter. Inside the subroutine, this reference is used to access the actual 

argument specified in the call. This means that changes made to the parameter will affect 

the argument used to call the subroutine.

By default, C# uses call-by-value, which means that a copy of the argument is made and 

given to the receiving parameter. Thus, when you pass a value type, such as 

int

 or 


double

,

what occurs to the parameter that receives the argument has no effect outside the method. 



For example, consider the following program:

// Value types are passed by value.

using System;

class Test {

  /* This method causes no change to the arguments

     used in the call. */

  public void NoChange(int i, int j) {

    i = i + j;

    j = -j;

  }


}

class CallByValue {

  static void Main() {

    Test ob = new Test();

www.freepdf-books.com



Download 4,07 Mb.

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