Conversion Operator

Why Trust Techopedia

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.

Advertisements

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.

Advertisements

Related Terms

Margaret Rouse
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…