C# Language Specification


Explicit dynamic conversions



Download 0,75 Mb.
bet153/524
Sana06.01.2022
Hajmi0,75 Mb.
#324842
1   ...   149   150   151   152   153   154   155   156   ...   524
Bog'liq
CSharp Language Specification

Explicit dynamic conversions


An explicit dynamic conversion exists from an expression of type dynamic to any type T. The conversion is dynamically bound (§7.2.2), which means that an explicit conversion will be sought at run-time from the run-time type of the expression to T. If no conversion is found, a run-time exception is thrown.

If dynamic binding of the conversion is not desired, the expression can be first converted to object, and then to the desired type.

Assume the following class is defined:

class C
{


int i;

public C(int i) { this.i = i; }

public static explicit operator C(string s)
{
return new C(int.Parse(s));
}
}

The following example illustrates explicit dynamic conversions:

object o = "1";


dynamic d = "2";

var c1 = (C)o; // Compiles, but explicit reference conversion fails


var c2 = (C)d; // Compiles and user defined conversion succeeds

The best conversion of o to C is found at compile-time to be an explicit reference conversion. This fails at run-time, because “1” is not in fact a C. The conversion of d to C however, as an explicit dynamic conversion, is suspended to run-time, where a user defined conversion from the run-time type of d – string – to C is found, and succeeds.



      1. Download 0,75 Mb.

        Do'stlaringiz bilan baham:
1   ...   149   150   151   152   153   154   155   156   ...   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