C# 0 The Complete Reference


PART I C h a p t e r   8



Download 4,07 Mb.
Pdf ko'rish
bet260/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   256   257   258   259   260   261   262   263   ...   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  

189


PART IPART I

(Return types do not provide sufficient information in all cases for C# to decide which 

method to use.) Of course, overloaded methods 

may

differ in their return types, too. When 

an overloaded method is called, the version of the method executed is the one whose 

parameters match the arguments.

Here is a simple example that illustrates method overloading:

// Demonstrate method overloading.

using System;

class Overload {

  public void OvlDemo() {

    Console.WriteLine("No parameters");

  }

  // Overload OvlDemo for one integer parameter.



  public void OvlDemo(int a) {

    Console.WriteLine("One parameter: " + a);

  }

  // Overload OvlDemo for two integer parameters.



  public int OvlDemo(int a, int b) {

    Console.WriteLine("Two parameters: " + a + " " + b);

    return a + b;

  }


  // Overload OvlDemo for two double parameters.

  public double OvlDemo(double a, double b) {

    Console.WriteLine("Two double parameters: " +

                       a + " "+ b);

    return a + b;

  }


}

class OverloadDemo {

  static void Main() {

    Overload ob = new Overload();

    int resI;

    double resD;

    // Call all versions of OvlDemo().

    ob.OvlDemo();

    Console.WriteLine();

    ob.OvlDemo(2);

    Console.WriteLine();

    resI = ob.OvlDemo(4, 6);

    Console.WriteLine("Result of ob.OvlDemo(4, 6): " + resI);

    Console.WriteLine();

    resD = ob.OvlDemo(1.1, 2.32);

    Console.WriteLine("Result of ob.OvlDemo(1.1, 2.32): " + resD);

  }

}

www.freepdf-books.com




190

 

P a r t   I :  



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

This program generates the following output:

No parameters

One parameter: 2

Two parameters: 4 6

Result of ob.OvlDemo(4, 6): 10

Two double parameters: 1.1 2.32

Result of ob.OvlDemo(1.1, 2.32): 3.42

As you can see, 

OvlDemo( )

 is overloaded four times. The first version takes no parameters

the second takes one integer parameter; the third takes two integer parameters; and the 

fourth takes two 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   256   257   258   259   260   261   262   263   ...   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