C# 0 The Complete Reference


PART I C h a p t e r   8



Download 4,07 Mb.
Pdf ko'rish
bet252/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   248   249   250   251   252   253   254   255   ...   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  

181


PART IPART I

Lcf of 231 and 105 is 3

Gcf of 231 and 105 is 21

No common factor for 35 and 51.



Use ref and out on References

The use of 



ref

 and 


out

 is not limited to the passing of value types. They can also be used 

when a reference is passed. When 

ref

 or 


out

 modifies a reference, it causes the reference, 

itself, to be passed by reference. This allows a method to change the object to which the 

reference refers. Consider the following program, which uses 



ref

 reference parameters to 

exchange the objects to which two references are referring:

// Swap two references.

using System;

class RefSwap {

  int a, b;

  public RefSwap(int i, int j) {

    a = i;

    b = j;

  }

  public void Show() {



    Console.WriteLine("a: {0}, b: {1}", a, b);

  }


  // This method changes its arguments.

  public void Swap(ref RefSwap ob1, ref RefSwap ob2) {

    RefSwap t;

    t = ob1;

    ob1 = ob2;

    ob2 = t;

  }

}

class RefSwapDemo {



  static void Main() {

    RefSwap x = new RefSwap(1, 2);

    RefSwap y = new RefSwap(3, 4);

    Console.Write("x before call: ");

    x.Show();

    Console.Write("y before call: ");

    y.Show();

    Console.WriteLine();

    // Exchange the objects to which x and y refer.

    x.Swap(ref x, ref y);

www.freepdf-books.com



182

 

P a r t   I :  



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

    Console.Write("x after call: ");

    x.Show();

    Console.Write("y after call: ");

    y.Show();

  }


}

The output from this program is shown here:

x before call: a: 1, b: 2

y before call: a: 3, b: 4

x after call: a: 3, b: 4

y after call: a: 1, b: 2

In this example, the method 

Swap( )

 exchanges the objects to which the two arguments to 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   248   249   250   251   252   253   254   255   ...   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