C# 0 The Complete Reference


GetOb( )  is called for an object of type  Gen2



Download 4,07 Mb.
Pdf ko'rish
bet673/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   669   670   671   672   673   674   675   676   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

GetOb( )

 is called for an object of type 



Gen2

, but the base class version is called for an object of type 



Gen

.

www.freepdf-books.com




542

 

P a r t   I :  



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

Notice one other thing: This line

iOb = new Gen2(99);

is valid because 



iOb

 is a variable of type 



Gen

. Thus, it can refer to any object of type 



Gen

 or any object of a class derived from 



Gen

, including 



Gen2

. Of course, 



iOb

 couldn’t be used to refer to an object of type 



Gen2

, for example, because of 

the type mismatch.

Overloading Methods That Use Type Parameters

Methods that use type parameters to declare method parameters can be overloaded. However, 

the rules are a bit more stringent than they are for methods that don’t use type parameters. 

In general, a method that uses a type parameter as the data type of a parameter can be 

overloaded as long as the signatures of the two versions differ. This means the type and/or 

number of their parameters must differ. However, the determination of type difference is 

not based on the generic type parameter, but on the type argument substituted for the type 

parameter when a constructed type is created. Therefore, it is possible to overload a method 

that uses type parameters in such a way that it “looks right,” but won’t work in all specific 

cases.


For example, consider this generic class:

// Ambiguity can result when overloading methods that

// use type parameters.

//

// This program will not compile.



using System;

// A generic class that contains a potentially ambiguous

// overload of the Set() method.

class Gen {

  T ob1;

  V ob2;


  // ...

  // In some cases, these two methods

  // will not differ in their parameter types.

  public void Set(T o) {

    ob1 = o;

  }


  public void Set(V o) {

    ob2 = o;

  }

}

class AmbiguityDemo {



  static void Main() {

    Gen ok = new Gen();

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   669   670   671   672   673   674   675   676   ...   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