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”:
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:
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 type | To Type(s) |
sbyte | byte, ushort, uint, ulong, char |
byte | sbyte, char |
short | sbyte, byte, ushort, uint, ulong, char |
ushort | sbyte, 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
- class Cat
- {
- public string Name;
- public int num_of_legs;
- }
- class Dog
- {
- public string Name;
- public int num_of_legs;
- public static explicit operator Dog(Cat c)
- {
- return new Dog
- {
- Name = c.Name,
- num_of_legs = c.num_of_legs
- };
- }
- }
- class DogAndCatConversion
- {
- static void Main(string[] args)
- {
- Cat c = new Cat
- {
- Name = "white cat",
- num_of_legs = 4
- };
- Dog d = (Dog)c; //explicitly casting from Cat to Dog.
- Console.WriteLine("Conversion succeeded");
- Console.ReadKey();
- }
- }
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
Add new comment
- 185 views