Chapter 2 Type Conversions

Type Conversions:

Type conversion is a process of converting one type into another. Using C# type conversion techniques, not only you can convert data types, you can also convert object types. The type conversion in C# can be either implicit conversion or an explicit conversion. If one type of data is automatically converted into another type of data, it is known as implicit conversions. There is no data loss due to implicit conversion. But explicit conversion is a forced conversion and there may be a data loss. Type conversions occur mainly when we pass arguments to a function or mixing mode arithmetic etc.

Implicit Type Conversion:

Byte datatype occupies one byte of memory for a value. Integer datatype occupies 4 bytes of memory. So we can copy byte to integer.

byte b =1;

int i= b;

Binary representation of above variable b is : 00000001

Binary representation of i in above variable is; 00000000 00000000 00000000 00000001

While at run time, when we use a byte value to a integer, one

When byte is converted to integer, first 3 values are filled by zeros automatically and the other value is left as is. In above example, since b is of byte datatype, it is represented as 00000001 in binary. When it is converted to integer, integer occupies 4 bytes, so first 3 octets are filled by zeros, and last octet has value. Here there is no data loss. If compiler is sure that the types are compatible, and no data loss occur, values can be converted to other data types implicitly.

You cannot convert an integer to a byte because, integer has 4 bytes in memory and byte has only one byte in memory. So there would be a data loss. It wont allow to run your script.

Here, you have to explicitly specify that you know about the data loss and you still want to convert. This is called Explicit conversion.

Explicit Type Conversion:

int i = 1;

byte b = i;

This conversion wont work by default as integer has 4 bytes, and byte has only one byte in memory. So, when integer is converted to byte, 3 octets would be removed. We will have data loss.

So you have to explicitly specify as:

byte b = (byte) i;

NOTE: This data loss doesnt happen all the time. When value of integer is more than the capacity of byte, data loss happens. Here, value of integer is 1, and biggest number that byte can store is 255. So, no data loss will happen. If we have integer value as 300, then there would be data loss.

This is also called casting.

Ex:

Float f = 1.0f;

int i = (int) f;

Non-Compatible Types:

If you want to convert a string to a integer, we cant do it, even if we specify explicitly. In this case, we have to use convert class given in .net framework or parse phrase. Toint32 is a .net framework which maps to integer in c#.

String s = “1”;

Int i = convert.toint32(s);

Byte is 1 byte, short is 2 bytes, int is 4 bytes, long is 8 bytes.

we are converting this value to integer. Integer has 4 bytes and each byte has 8 bits. So, 8*4 = 32. So we use .toint32 class.

If you are converting from short datatype, you have to use toint16, because short has 2 bytes, and each byte has 8 bits. So, 8*2 = 16. So we have to use toint16 class.

We can also use parse() method which converts string to desired data type as:

String s = 1;

Int j = int.parse(s);

ToByte() - converts to byte (1 byte)

ToInt16() - converts to Short ( 2 bytes)

ToInt32() - Converts to Integer ( 4 bytes)

ToInt64() - Converts to Long (8 bytes)

int g = 900; byte k = (byte)g; Console.WriteLine(k); Here, k shows 132 as some of the binary values are lost.

Overflow example:

String s = "1000"; Byte k = Convert.ToByte(s); Console.WriteLine(k); Here we are trying to convert a string, 1000 to a byte. Max value that byte an store is 255. We are trying to convert 1000, so it throws an exception and stops.

Try catch:

Instead of letting script to crash as above, we can use try catch, to grab the error and stop application from crashing.

try { String s = "1000"; Byte k = Convert.ToByte(s); Console.WriteLine(k); } catch { Console.WriteLine("hhhh"); }

Note: You can only convert string to int when that string value is a integer.

Ex:

string s = "k"; int k = Convert.ToInt16(s); Console.WriteLine(k);

This doesn't work as 'k' cannot be converted to integer using toint16. but if you use string s = "1"; in above example, it will work as 1 is already integer but it is specified as string. When converted back to integer it works.

Last updated