C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet316/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   312   313   314   315   316   317   318   319   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

true

and


false

 for a class, you enable the compiler to utilize the short-circuit operators without 

having to explicitly overload either. Furthermore, you gain the ability to use objects in 

conditional expressions. In general, unless you need a very narrow implementation of 



&

and


|

, you are better off creating a full implementation.



Conversion Operators

In some situations, you will want to use an object of a class in an expression involving other 

types of data. Sometimes, overloading one or more operators can provide the means of doing 

this. However, in other cases, what you want is a simple type conversion from the class type 

to the target type. To handle these cases, C# allows you to create a special type of 

operator

method called a 



conversion operator.

 A conversion operator converts an object of your class into 

another type. Conversion operators help fully integrate class types into the C# programming 

environment by allowing objects of a class to be freely mixed with other data types as long 

as a conversion to those other types is defined.

There are two forms of conversion operators, implicit and explicit. The general form for 

each is shown here:

public static operator implicit 



target-type

(

source-type v

) { return 

value

; }


public static operator explicit 

target-type

(

source-type v

) { return 

value

; }


Here, 

target-type

 is the target type that you are converting to; 



source-type

 is the type you 

are converting from; and 

value

 is the value of the class after conversion. The conversion 

operators return data of type 

target-type,

 and no other return type specifier is allowed.

If the conversion operator specifies 

implicit

, then the conversion is invoked automatically, 

such as when an object is used in an expression with the target type. When the conversion 

operator specifies 



explicit

, the conversion is invoked when a cast is used. You cannot define 

both an implicit and explicit conversion operator for the same target and source types.

To illustrate a conversion operator, we will create one for the 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   312   313   314   315   316   317   318   319   ...   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