ALERT

[WEBINAR] Application Running Slowly? Time to Get Precise

Conversion Operator

Definition - What does Conversion Operator mean?

A conversion operator, in C#, is an operator that is used to declare a conversion on a user-defined type so that an object of that type can be converted to or from another user-defined type or basic type. The two different types of user-defined conversions include implicit and explicit conversions.

In general, explicit and implicit conversion operators provide the capability to a class to be cast to the other possible data types.

An explicit conversion operator must be invoked with a cast and is used when the conversion has to be visible to the operator's users. It is used in cast expressions where the two data types are not entirely compatible and therefore require a cast operator.

An implicit conversion operator is easy to use. It does not require special syntax and it improves the code readability. It also helps a class to convert data of a data type to its compatible type without type casting. Implicit cast has to be used in situations like function member invocations and assignments, where there is no risk of data loss or the occurrence of exceptions.

Techopedia explains Conversion Operator

An implicit conversion can be defined for a class using the keyword "implicit" along with the "operator" keyword. An explicit conversion operation can be defined for a class using the keyword "explicit" along with the "operator" keyword. Both the conversions must be defined as static.

For example, a class, RomanNumeral, can be defined with two conversion operators. An implicit conversion operator can be defined to convert from RomanNumeral class to string for displaying a number in Roman numeral form; the explicit conversion operator can be defined to perform the conversion from RomanNumeral class to integer.

Conversion of a class to object type or interface type is not allowed. The same goes for conversion from base class to a class derived from base class.

For a given class, both the explicit and implicit operators cannot be specified for conversion from the same type to another.

Care should be taken to ensure that an implicit conversion does not result in data loss or an exception. If there are valid reasons to throw exception, the conversion should be of explicit type.

This definition was written in the context of C#

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
"Techopedia" on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Resources
JOIN US AT THWACKCAMP 2016
JOIN US AT THWACKCAMP 2016:
THWACKcamp is an annual IT knowledge share event hosted by THWACK, the SolarWinds community of 130,000+ members. It's a two day event (September 14 -...
Free Whitepaper: The Path to Hybrid Cloud
Free Whitepaper: The Path to Hybrid Cloud:
The Path to Hybrid Cloud: Intelligent Bursting To Amazon Web Services & Microsoft Azure
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomics
Free 30 Day Trial – Turbonomics:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.