Cisco CloudCenter: Get the Hybrid IT Advantage

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#
Share this:

Connect with us

Email Newsletter

Join thousands of others with our weekly newsletter

The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
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 – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.