What is type conversion?
When a variable is declare and a value assigned to it, the compiler checks at compiled time whether the value being assigned to the variable will fit into the space allocated in memory to the variable. The type of the variable never changes during the scope of the program but its value can be used by other variables of a similar type with smaller or bigger space.
When you try to copy value of one variable to a different type some kind of conversion may be required.
Forms of Conversion
Implicit Conversion
This type of conversion happens when a value being assigned in not bigger that the containing and is from the same branch. If you assign an integer value to a long variable an implicit conversion would happen. Suffice to say that the compiler would be happy to convert the int value to a long type and assign it to the variable of type long.
int x = 90899909; //int is 4 byte
long y = x; //long is 8 byte
Explicit Conversion
This type of conversion requires the use of cast to tell the compiler that you required the conversion at your own risk of data loss. In this case you are assigning a value to a memory than does not fully fit and may get lost. When this is the case you need to explicitly cast the incoming value before assigning it to a smaller variable.
You need to keep in mind that the target variable has a smaller memory so as long as the value you are assigning to it is within its range your code will work. If the value is greater than the range of the destination after conversion an arithmetic overflow may occur.
An explicit conversion may also cause precision loss by rounding off values as we see in the example below that the decimal part (0.123) is lost.
You need to keep in mind that the target variable has a smaller memory so as long as the value you are assigning to it is within its range your code will work. If the value is greater than the range of the destination after conversion an arithmetic overflow may occur.
An explicit conversion may also cause precision loss by rounding off values as we see in the example below that the decimal part (0.123) is lost.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace aamirmehmood.tutorials.csharp
{
class Program
{
static void Main(string[] args)
{
FloatToInteger();
}
private static void FloatToInteger()
{
float f = 2.123f;
int x = (int)f;
Console.WriteLine(string.Format("Value of f = {0}",f));
Console.WriteLine(string.Format("Value of x = {0}", x));
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace aamirmehmood.tutorials.csharp
{
class Program
{
static void Main(string[] args)
{
FloatToInteger();
}
private static void FloatToInteger()
{
float f = 2.123f;
int x = (int)f;
Console.WriteLine(string.Format("Value of f = {0}",f));
Console.WriteLine(string.Format("Value of x = {0}", x));
Console.ReadLine();
}
}
}
Output
Value of f = 2.123Value of x = 2
Table showing numeric variable that require explicit conversions
(source MSDN: https://msdn.microsoft.com/en-us/library/yht2cx7b.aspx)
From To
sbyte byte, ushort, uint, ulong, or char
byte Sbyte or char
short sbyte, byte, ushort, uint, ulong, or char
ushort sbyte, byte, short, or char
int sbyte, byte, short, ushort, uint, ulong, or char
uint sbyte, byte, short, ushort, int, or char
long sbyte, byte, short, ushort, int, uint, ulong, or char
ulong sbyte, byte, short, ushort, int, uint, long, or char
char sbyte, byte, or short
float sbyte, byte, short, ushort, int, uint, long, ulong, char, or decimal
double sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or decimal
decimal sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double
Conversions using built in methods
C# defines helper classes that you can use to convert variable from one type to the other type.
The System.Convert class provides many methods which you can use to convert one base data type to the other base data type.
Example of a using Convert class to convert a string to DateTime data type variable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace aamirmehmood.tutorials.csharp
{
class Program
{
static void Main(string[] args)
{
ConvertToDate("12-Oct-2016");
}
private static void ConvertToDate(string dateString)
{
DateTime date = Convert.ToDateTime(dateString);
Console.WriteLine(string.Format("Value of date = {0}", date.ToString()));
Console.ReadLine();
}
}
}
No comments:
Post a Comment