Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Explicit and Implicit Type Casting, and the Difference

Type conversion comes into the life of every developer and clear conversions help the application to be less error-prone and easier to maintain.

Aug 6, 2019 • 6 Minute Read

Introduction

There are two main types of programming languages. The first one is statically typed, the second one is dynamically typed. Dynamic typing is sometimes also called Duck typing. There is a famous story verb for that.

If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

We are going to take a look at the features provided by C# regarding static typing. In C#, when a variable is declared it cannot be declared again or assigned a value of another type unless that type is implicitly convertible to the given variable type. This is decided during compilation time and poses a restriction as to how a developer would use/declare variables. Although it also comes with a speedbump in execution, statically typed languages are usually faster than dynamically typed ones. Both have their advantages and disadvantages.

Let's take a simple example.

      int j;
j = "Hello C# World";
    

This will give us an error because int and string are not implicitly convertible.

There might come a time when you need to copy the value of a variable to another, like passing an integer to a method which takes double or long arguments. Or you may need to push variables between classes as well.

C# provides four ways of converting one type to another:

  1. Implicit conversion
  2. Explicit conversion
  3. User-defined conversion
  4. Conversion with helper classes

Type conversion is also called casting or type casting.

Implicit Conversions

There is no special syntax for this type of conversion, this is the safest type of casting. No data is lost, for example, when converting from smaller to larger integral types or derived classes to base classes.

There is an implicit numeric conversions table which comes in handy when you want to figure out which types are inter-compatible.

Below is an Example of implicit conversion of numeric datatypes.

      int number = 128;
double bigger = number;
    

When you are working with double type, you need to be aware of the fact that it will be rounded up to 15 significant digits before any formatting is applied.

Below is an example of implicit conversion of class to base-class.

      DerivedClass d = new DerivedClass()
BaseClass b = d
    

There are a number of conversion sub-types which are all considered implicit.

  1. Identity conversions
  2. Implicit numeric conversions
  3. Implicit enumeration conversions
  4. Implicit nullable conversions
  5. Null literal conversions
  6. Implicit reference conversions
  7. Boxing conversions
  8. Implicit dynamic conversions
  9. mplicit constant expression conversions
  10. User-defined implicit conversions
  11. Anonymous function conversions
  12. Method group conversions

Explicit Conversions

This type of casting also involves a cast operator. The process is usually associated with information loss or failure to convert between types. Typically, numeric conversions are mentioned when we convert from a larger precision datatype to a lower precision. Sometimes even converting from a base class to a derived class is also mentioned here.

There is an explicit numeric conversions table which comes in handy when you want to figure out which types are compatible.

Below is an example of explicit conversion.

      double mypi = 3.14;
int a;
a = (int)mypi;
    

Note the cast operator which is the (int). This conversion means the result will be stripped of all its digits and the result is 3.

There is also a possibility to convert a base class to a derived class.

      Opel o = new Opel();
Car c = o;
Opel oo = (Opel)c;
    

Conversion Augmentation

C# allows you to augment the pre-defined implicit and explicit conversions, these are called user-defined conversions. They can be introduced by declaring conversion operators in class and struct types. There is a strict restriction as to what can be a user-defined conversion. You are not allowed to redefine any existing implicit or explicit conversions.

On this site you can read in more detail about as to how you can define your own conversions.

These user-defined conversions convert a value from its type, called source type, to another type which is called target type. The evaluation is done by finding the most specific user-defined conversion operator for the particular source and target types.

Steps in general:

  1. Finds the set of classes or structs from which this conversion operator will be considered.
  2. Formats the set of types and determines which operators are applicable.
  3. From the set of applicable operators, determine which is the most specific while being unambiguous.
  4. If necessary, performs the standard conversion.
  5. Invokes the user-defined conversion.
  6. If necessary, finally perform the standard conversion.

Conclusion

Type conversion comes into the life of every developer, once in a while. This is a very easy concept to understand. There are a few rules you need to be aware of and, apart from that, the experience comes with practice. Clear conversions help the application to be more error-prone, and easier to maintain. They also reduce the time spent debugging syntactically correct programs hunting for semantic errors. All in all, I hope this has been a worthwhile reading and it gave you the insight you were looking for!