Tag Archives: Type conversion

Type conversion in C#

Type conversion (or typecasting) is the technique to change an entity from one data type into another. In general there are two types of type conversion; implicit and explicit conversion. This post tells about type conversion in C#.NET and the performance issues.

Implicit conversion

Implicit conversion (or coercion) is automatic type conversion by the compiler. In C# it does not require a special syntax because the type conversion is safe and no data will be lost. In the following example the value of an integer is stored in a long. This is safe because an integer is 4 bytes (on a 32 bit computer), and a long is 8 bytes. So every value that fits in an integer fits in a long. For a complete conversion table see this page.

It is also possible to cast a derived class to a base class without using a special syntax.
Continue reading Type conversion in C#