Converting Data Types in C# Language

Objective

This tutorial will explain you to understand how and why a conversion of data type occur in C#. Understanding the conversion will help you to avoid error and lose data when operation happen.

Let's go

When you copy a value from one variable to another variable, a conversion must occur. Also when you want to do a operation on two difference data types, a conversion might also need occur. There are two types of conversion: implicit and explicit conversion.

Implicit Conversion

Implicit Conversions happen automatically without error. The program (and C# language) do it for you without any declaration. For example, when you copy a interger value from a variable of type int to a variable of type long, you just write “nature”:

int x = 123;
double y = x; //auto conversion from int type to long type

Explicit conversions are conversions of data that are forced. For the almost value data types, when you want to do a conversion, you need to cast them from destination type to target type. A cast is the forcing of value from a data type to another data type. The format of a cast is show bellow: To_variable = (datatype) from_variable; Datatype is the type of data that from_variable belong to. Bellow are examples of explicit conversion:

double y = 123;
int x = (int)y;

Here we have to target the type operator (int) when converting from double type to int type else the compiler will throw an error and the application will be crashed. When doing casts, you must make sure that the target variable can hold the value of destination variable that are converted. If the receiving variable cannot store the received value, truncation or other changes can occur. The table bellow show the required explicit conversion

From typeTo Type(s)
sbyte byte, ushort, uint, ulong, char
bytesbyte, char
shortsbyte, byte, ushort, uint, ulong, char
ushortsbyte, byte, short, char
int sbyte, byte, short, ushort, uint, ulong, char
uint sbyte, byte, short, ushort, int, char
long sbyte,byte, short, ushort, int, uint, ulong, char
ulong sbyte,byte, short, ushort, int, uint, long, char
char sbyte, byte, short
float sbyte,byte, short, ushort, int, uint, ulong, char, decimal
double sbyte,byte, short, ushort, int, uint, ulong, char, float, decimal
decimal sbyte,byte, short, ushort, int, uint, ulong, char, float, double

Conversion Operators

Conversion operators help to cast user-defined types from one type to the other, usually the basic types. For both implicit and explicit conversion, we need to create a static method in the corresponding class (target class) with method name as the type it returns including the keyword that says implicit or explicit. Bellow is example of how to implement a conversion operator

  1. class Cat
  2. {
  3. public string Name;
  4. public int num_of_legs;
  5. }
  6.  
  7. class Dog
  8. {
  9. public string Name;
  10. public int num_of_legs;
  11. public static explicit operator Dog(Cat c)
  12. {
  13. return new Dog
  14. {
  15. Name = c.Name,
  16. num_of_legs = c.num_of_legs
  17. };
  18. }
  19. }
  20.  
  21. class DogAndCatConversion
  22. {
  23. static void Main(string[] args)
  24. {
  25. Cat c = new Cat
  26. {
  27. Name = "white cat",
  28. num_of_legs = 4
  29. };
  30. Dog d = (Dog)c; //explicitly casting from Cat to Dog.
  31. Console.WriteLine("Conversion succeeded");
  32. Console.ReadKey();
  33. }
  34. }

Explanation

In this sample, we want demo how to convert from Cat type to Dog type.

  • The dog and cat no need have same data, here for simple purpose, we assume they have same two data, Name and Num_of_Legs.
  • To convert from Cat Type to Dog Type, in Dog class we need to declare a static operator with explicit keyword included. The name of operator is the same as the class name (Dog), but the parameter has type of Cat.
  • In operator, we just return an object that get all data from parameter.
  • In Main function when we need convert a Cat object to Dog object, simply write an explicit convertion like we’ve seen above.

Summary

So far we have investigated conversion - an important field of C# language – includes:

  • Implicit conversion
  • Explicit conversion
  • Conversion operators
Conversion is used in C# program very often, so you need to learn it carefully to make sure not getting error on program.

Add new comment