Type Conversions In C#

Type conversion is a process of converting one type into another. Using C# type conversion techniques, not only can you convert data types but you can also convert object types. The type conversion in C# can be either implicit conversion or an explicit conversion. If one type of data is automatically converted into another type of data, it is known as implicit conversion. There is no data loss due to implicit conversion. But explicit conversion is a forced conversion and there may be a data loss. Type conversions occur mainly when we pass arguments to a function or mixing mode arithmetic etc.

Implicit Conversions in C#

The implicit conversions can occur in a variety of situations like function invoking, cast expressions, assignments etc. The implicit conversions can be further classified into different categories.

Implicit Numerical Conversions 

The possible implicit numerical conversions in C# are shown below.

Type Conversions In C#

The implicit conversions in the direction of the arrow are possible with basic data types of C#. Remember that there are no implicit conversions to the char type from any other types. Also, there are no implicit or explicit conversions associated with the bool type. The examples of implicit numerical conversions are shown below.

  1. long x;  
  2. int y = 25;  
  3. x = y; //implicit numerical conversion  

Implicit Enumeration Conversion

An implicit enumeration conversion permits the decimal integer literal 0 to be converted to an enum type.

Implicit Reference Conversion

The possible implicit reference conversions are:

  1. From any reference type to object.
  2. From any class type D to any class type B, provided D is inherited from B.
  3. From any class type A to interface type I, provided A implements I.
  4. From any interface type I2 to any other interface type I1, provided I2 inherits I1.
  5. From any array type to System.Array.
  6. From any array type A with element type a to an array type B with element type b provided A & B differ only in element type (but the same number of elements) and both a and b are reference types and an implicit reference conversion exists between a & b.
  7. From any delegate type to System.Delegate type.
  8. From an array type or delegate type to System.ICloneable.
  9. From null type to any reference type.

Boxing Conversions

Boxing is the conversion of any value type to object type. Remember that boxing is an implicit conversion. Boxing a value of value type like int consists of allocating an object instance and copying the value of the value type into that object instance. An example of boxing is shown below.

  1. int x = 10;  
  2. object o = x; //Boxing  
  3. if(o is int)  
  4. Console.WriteLine("o contains int type");  

A boxing conversion is making a copy of the value being boxed. But when we convert a reference type to object type, the object continues to reference the same instance.

Explicit Conversions in C# 

The explicit conversions are forced conversions in C# by using the casting operator (). There may be data loss due to explicit conversions. For example when we explicitly convert a double type to an int type as shown below:

  1. int x = (int) 26.45; //Explicit conversion  
  2. Console.WriteLine(x); // Displays only 26  

Explicit Numerical Conversions 

They are the conversions from a numeric type to another numeric type for which an implicit conversion doesn't already exist. It is possible to convert any numerical type to any other numerical type explicitly. The explicit numerical conversions can result in possibly a data loss or OverflowException to be thrown.

Explicit Enumeration Conversions

The explicit enumeration conversions are,

  1. From sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double or decimal to any enum type.
  2. From any enum type to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double or decimal.
  3. From any enum type to any other enum type.

Explicit Reference Conversions 

The possible explicit reference conversions are,

  1. From object to any reference type.
  2. From any class type B to any class type D, provided B is the base class of D
  3. From any class type A to any interface type I provided S is not sealed and do not implement I.
  4. From any interface type I to any class type A provided A is not sealed and implement I.
  5. From any interface type I2 to any interface type I1, provided I2 is not derived from I1.
  6. From System.Array to an array type.
  7. From System.Delegate type to any delegate type.
  8. From System.ICloneable to any array or delegate type.

For any reason, if an explicit reference conversion fails, an InvalidCastException is thrown.

Un-boxing Conversions

Un-boxing is the conversion of an object type to a value type. The casting operator () is necessary for unboxing. The example of unboxing is shown below.

  1. int x = 100;  
  2. object o = x; // Boxing  
  3. int I (int) o; // un-boxing  

For an unboxing conversion to a given value type to succeed at run-time, the value type of the source argument must be reference type to an object that was previously created by boxing a value of the value type. If the source argument is null or a reference type to an incompatible type, an InvalidCastException is thrown.

User-Defined Conversions

Whatever we explained so far are the conversions (implicit or explicit) among the basic data types or among the same user-defined data types. Suppose, we have to do conversions between basic data types and user-defined data types or between two incompatible user-defined data types. C# provides a facility to define conversion operators inside a class or struct to achieve this.

But C# provides only certain user-defined conversions to be defined. In particular, it is not possible to redefine an existing implicit or explicit conversion in C#. A class or struct is permitted to declare a conversion function from a source type S to a target type T only if all of the following are true,

  1. Both S & T are different types.
  2. Either S or T is a class or struct type in which the operator declaration takes place.
  3. Neither S nor T is an object type or an interface type.
  4. T is not a base class of S or S is not a base class of T.

The user-defined conversion can either implicit or explicit. The general form of user-defined conversion operator is as follows,

  1. public static implicit/explicit operator type (arguments)  
  2. {   
  3. }  

Where the operator is the required keyword and type is the required return type, remember that operator function should be public and static and there is no return type. The presence of the explicit keyword makes the conversion as explicit and implicit keyword makes the conversion as implicit.

The conversion operator can be defined either inside the class or struct type. Remember that user-defined conversions are not allowed to convert from or to interface types. Also note that since explicit or implicit keywords are not part of the method's signature, it is not possible to declare both explicit and implicit conversion operator with the same source and target type.

An example of a user-defined conversion is shown below.
  1. // Conversions  
  2. // Author: [email protected]  
  3. using System;  
  4. class MyDigit {  
  5.     private int x;  
  6.     public MyDigit() {}  
  7.     public MyDigit(int i) {  
  8.         x = i;  
  9.     }  
  10.     public void ShowDigit {  
  11.         Console.WriteLine("{0}", x);  
  12.     }  
  13.     public static implicit operator int(MyDigit md) {  
  14.         return md.x;  
  15.     }  
  16.     public static explicit operator MyDigit(int val) {  
  17.         return new MyDigit(val);  
  18.     }  
  19. }  
  20. class MyClient {  
  21.     public static void Main() {  
  22.         MyDigit md1 = new MyDigit1(10);  
  23.         int x = md1; //Implicit  
  24.         Console.WriteLine(x); //Displays 10  
  25.         int y = 25;  
  26.         MyDigit md2 = (MyDigit) y; //Explicit  
  27.         Console.WriteLine(md2.ShowDigit());  
  28.     }  
  29. }
The above example converts a basic type to a class type and a class type to basic type by using the conversion operator. The conversion operator can be used to convert one class type to another also. An example is shown below.

  1. // Conversions  
  2. // Author: [email protected]  
  3. using System;  
  4. class MyClass1 {  
  5.     public int x;  
  6.     public MyClass1(int a) {  
  7.         x = a;  
  8.     }  
  9.     public void Show1() {  
  10.         Console.WriteLine(x);  
  11.     }  
  12.     public static explicit operator MyClass2(MyClass1 mc1) {  
  13.         MyClass2 mc2 = new MyClass2(mc1.x * 10, mc1.x * 20);  
  14.         return mc2;  
  15.     }  
  16. }  
  17. class MyClass2 {  
  18.     public float x, y;  
  19.     public MyClass2(float a, float b) {  
  20.         x = a;  
  21.         y = b;  
  22.     }  
  23.     public void Show2() {  
  24.         Console.WriteLine(x);  
  25.         Console.WriteLine(y);  
  26.     }  
  27. }  
  28. class MyClient {  
  29.     public static void Main() {  
  30.         MyClass1 mc1 = new MyClass1(100);  
  31.         mc1.Show1();  
  32.         MyClass2 mc2 = (MyClass2) mc1;  
  33.         mc2.Show2();  
  34.     }  
  35. }  
If a user-defined conversion can give rise to exceptions or loss of information, then that conversion should be defined as an explicit conversion. In general user defined implicit conversions should be designed to never throw exceptions and never lose information. Remember that user defined conversions can be either defined inside source class or inside a target class in the case of class-to-class conversions.


Similar Articles