C# 0 The Complete Reference


Pass References to Methods



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

Pass References to Methods

Up to this point, the examples in this book have been using value types, such as 



int

 or 


double

, as parameters to methods. However, it is both correct and common to use a 

reference type as a parameter. Doing so allows an object to be passed to a method. For 

example, consider the following program:

// References can be passed to methods.

using System;

class MyClass {

  int alpha, beta;

www.freepdf-books.com



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  

173


PART IPART I

  public MyClass(int i, int j) {

    alpha = i;

    beta = j;

  }

  // Return true if ob contains the same values as the invoking object.



  public bool SameAs(MyClass ob) {

    if((ob.alpha == alpha) & (ob.beta == beta))

       return true;

    else return false;

  }

  // Make a copy of ob.



  public void Copy(MyClass ob) {

    alpha = ob.alpha;

    beta  = ob.beta;

  }


  public void Show() {

    Console.WriteLine("alpha: {0}, beta: {1}",

                      alpha, beta);

  }


}

class PassOb {

  static void Main() {

    MyClass ob1 = new MyClass(4, 5);

    MyClass ob2 = new MyClass(6, 7);

    Console.Write("ob1: ");

    ob1.Show();

    Console.Write("ob2: ");

    ob2.Show();

    if(ob1.SameAs(ob2))

      Console.WriteLine("ob1 and ob2 have the same values.");

    else


      Console.WriteLine("ob1 and ob2 have different values.");

    Console.WriteLine();

    // Now, make ob1 a copy of ob2.

    ob1.Copy(ob2);

    Console.Write("ob1 after copy: ");

    ob1.Show();

    if(ob1.SameAs(ob2))

      Console.WriteLine("ob1 and ob2 have the same values.");

    else

      Console.WriteLine("ob1 and ob2 have different values.");

  }

}

www.freepdf-books.com




174

 

P a r t   I :  



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

This program generates the following output:

ob1: alpha: 4, beta: 5

ob2: alpha: 6, beta: 7

ob1 and ob2 have different values.

ob1 after copy: alpha: 6, beta: 7

ob1 and ob2 have the same values.

The



Download 4,07 Mb.

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