C# Language Specification


Implementation of generic methods



Download 0,75 Mb.
bet449/524
Sana06.01.2022
Hajmi0,75 Mb.
#324842
1   ...   445   446   447   448   449   450   451   452   ...   524
Bog'liq
CSharp Language Specification

Implementation of generic methods


When a generic method implicitly implements an interface method, the constraints given for each method type parameter must be equivalent in both declarations (after any interface type parameters are replaced with the appropriate type arguments), where method type parameters are identified by ordinal positions, left to right.

When a generic method explicitly implements an interface method, however, no constraints are allowed on the implementing method. Instead, the constraints are inherited from the interface method

interface I
{
void F(T t) where T: A;
void G(T t) where T: B;
void H(T t) where T: C;
}

class C: I


{
public void F(T t) {...} // Ok
public void G(T t) where T: C {...} // Ok
public void H(T t) where T: string {...} // Error
}

The method C.F implicitly implements I.F. In this case, C.F is not required (nor permitted) to specify the constraint T: object since object is an implicit constraint on all type parameters. The method C.G implicitly implements I.G because the constraints match those in the interface, after the interface type parameters are replaced with the corresponding type arguments. The constraint for method C.H is an error because sealed types (string in this case) cannot be used as constraints. Omitting the constraint would also be an error since constraints of implicit interface method implementations are required to match. Thus, it is impossible to implicitly implement I.H. This interface method can only be implemented using an explicit interface member implementation:

class C: I
{
...

public void H(U u) where U: class {...}

void I.H(T t) {
string s = t; // Ok
H(t);
}
}

In this example, the explicit interface member implementation invokes a public method having strictly weaker constraints. Note that the assignment from t to s is valid since T inherits a constraint of T: string, even though this constraint is not expressible in source code.



      1. Download 0,75 Mb.

        Do'stlaringiz bilan baham:
1   ...   445   446   447   448   449   450   451   452   ...   524




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