Implicit Conversion in C#

 

We can easily convert one data type variable to another type of variable in C#. There are different type conversions  categorized under implicit conversion. Those are

 

  •  Identity conversions
  •  
  •  Implicit numeric conversions
  •  
  •  Implicit enumeration conversions.
  •  
  •  Implicit reference conversions
  •  
  •  Boxing conversions
  •  
  •  Implicit constant expression conversions
  •  
  •  User-defined implicit conversions

 

Implicit conversions can occur in a variety of situations, including function member invocations (§7.4.3), cast expressions and assignments.

 

The predefined implicit conversions always succeed and never cause exceptions to be thrown. Properly designed user-defined implicit conversions should exhibit these characteristics as well.

 

Identity conversion:

An identity conversion converts from any type to the same type. This conversion exists only such that an entity that already has a required type can be said to be convertible to that type.

 

Implicit numeric conversions:

The implicit numeric conversions are:

 

  •   From sbyte to short, int, long, float, double, or decimal.
  •  
  •  From byte to short, ushort, int, uint, long, ulong, float, double, or decimal.
  •  
  •  From short to int, long, float, double, or decimal.
  •  
  •  From ushort to int, uint, long, ulong, float, double, or decimal.
  •  
  •  From int to long, float, double, or decimal.
  •  
  •  From uint to long, ulong, float, double, or decimal.
  •  
  •  From long to float, double, or decimal.
  •  
  •  From ulong to float, double, or decimal.
  •  
  •  From char to ushort, int, uint, long, ulong, float, double, or decimal.
  •  
  •  From float to double.

 

Conversions from int, uint, or long to float and from long to double may cause a loss of precision, but will never cause a loss of magnitude. The other implicit numeric conversions never lose any information.

 

There are no implicit conversions to the char type. This in particular means that values of the other integral types do not automatically convert to the char type.

 

Implicit enumeration conversions:

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

 

Implicit reference conversions:

The implicit reference conversions are:

 

  •  From any reference-type to object.
  •  
  •  From any class-type S to any class-type T, provided S is derived from T.
  •  
  •  From any class-type S to any interface-type T, provided S implements T.
  •  
  •  From any interface-type S to any interface-type T, provided S is derived from T.
  •  
  •  From an array-type S with an element type SE to an array-type T with an element type TE, provided all of the following are true:

                     S and T differ only in element type. In other words, S and T have the same number of dimensions.

                                Both SE and TE are reference-types.

                         An implicit reference conversion exists from SE to TE.
 

  •  From any array-type to System.Array.
  •  
  •  From any delegate-type to System.Delegate.
  •  
  •  From any array-type or delegate-type to System.ICloneable.
  •  
  •  From the null type to any reference-type.

The implicit reference conversions are those conversions between reference-types that can be proven to always succeed, and therefore require no checks at run-time.

 

Reference conversions, implicit or explicit, never change the referential identity of the object being converted. In other words, while a reference conversion may change the type of a value, it never changes the value itself.

 

Boxing conversions:

A boxing conversion permits any value-type to be implicitly converted to the type object or to any interface type implemented by the value-type. Boxing a value of a value-type consists of allocating an object instance and copying the value-type value into that instance.

 

Implicit constant expression conversions:

An implicit constant expression conversion permits the following conversions:

 

  •  A constant-expression of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.
  •  A constant-expression of type long can be converted to type ulong, provided the value of the constant expression is not negative.

 

User-defined implicit conversions:

A user-defined implicit conversion consists of an optional standard implicit conversion, followed by execution of a user-defined implicit conversion operator, followed by another optional standard implicit conversion.