C# 0 The Complete Reference


MyMeth( )  are defined: one that has an  int



Download 4,07 Mb.
Pdf ko'rish
bet263/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   259   260   261   262   263   264   265   266   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

MyMeth( )

 are defined: one that has an 



int

 parameter 

and one that has a 

double

 parameter. However, it is possible to pass 



MyMeth( )

 a 


byte

,

short

, or 

float

 value. In the case of 



byte

 and 


short

, C# automatically converts them to 



int

.

Thus,



MyMeth(int)

 is invoked. In the case of 



float

, the value is converted to 



double

 and 


MyMeth(double)

 is called.

It is important to understand, however, that the implicit conversions apply only if there 

is no exact type match between a parameter and an argument. For example, here is the 

preceding program with the addition of a version of 

MyMeth( )

 that specifies a 



byte

parameter:

// Add MyMeth(byte).

using System;

class Overload2 {

  public void MyMeth(byte x) {

    Console.WriteLine("Inside MyMeth(byte): " + x);

  }


  public void MyMeth(int x) {

    Console.WriteLine("Inside MyMeth(int): " + x);

  }

  public void MyMeth(double x) {



    Console.WriteLine("Inside MyMeth(double): " + x);

  }


}

www.freepdf-books.com




192

 

P a r t   I :  



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

class TypeConv {

  static void Main() {

    Overload2 ob = new Overload2();

    int i = 10;

    double d = 10.1;

    byte b = 99;

    short s = 10;

    float f = 11.5F;

    ob.MyMeth(i); // calls ob.MyMeth(int)

    ob.MyMeth(d); // calls ob.MyMeth(double)

    ob.MyMeth(b); // calls ob.MyMeth(byte) -- now, no type conversion

    ob.MyMeth(s); // calls ob.MyMeth(int) -- type conversion

    ob.MyMeth(f); // calls ob.MyMeth(double) -- type conversion

  }

}

Now when the program is run, the following output is produced:



Inside MyMeth(int): 10

Inside MyMeth(double): 10.1

Inside MyMeth(byte): 99

Inside MyMeth(int): 10

Inside MyMeth(double): 11.5

In this version, since there is a version of 



MyMeth( )

 that takes a 



byte

 argument, when 



MyMeth( ) 

is called with a 



byte

 argument, 



MyMeth(byte)

 is invoked and the automatic 

conversion to 

int

 does not occur.

Both

ref

 and 


out

 participate in overload resolution. For example, the following defines 

two distinct and separate methods:

public void MyMeth(int x) {

  Console.WriteLine("Inside MyMeth(int): " + x);

}

public void MyMeth(ref int x) {



  Console.WriteLine("Inside MyMeth(ref int): " + x);

}

Thus,



ob.MyMeth(i)

invokes


MyMeth(int x)

, but


ob.MyMeth(ref i)

invokes


MyMeth(ref int x)

.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   259   260   261   262   263   264   265   266   ...   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