You currently have JavaScript disabled on your browser.

This website uses JavaScript, and This page needs JavaScript activated to work correctly.

Please active JavaScript on your browser and then refresh this page.

adThumb

Journal Entries Of Agent Adams

Episodes 1 thru 3 are $FREE      buyNow
The classified reports of a CIA field agents investigations into various unusual events, paranormal activities, and other strange phenomena.

Type Casting



Type Casting is a process of converting the value of one data type into another data type (for example: converting a number into a string, or converting a string into a number).

The two main methods of Type Casting are:

Note: Be aware that when type casting a value from one variable type to another variable type, the precision of the value may change due to rounding and/or trimming of the value, so that the converted value can fit into the desired variable type.

Implicit Casting

With Implicit Casting, the conversion of the value is performed automatically whenever the value is passed from a smaller size variable type to a larger size variable type:

 
int myInt = 12;
double myDouble = myInt;      // Automatic casting of converting an int into a double

Console.WriteLine(myInt);     // outputs 12
Console.WriteLine(myDouble);  // outputs 12

Explicit Casting

With Explicit Casting, the conversion of the value is only performed whenever manually told to do so. There are two ways in which you can manually perform explicit casting:


Manual Method

Explicit casting using the manual method is performed by placing the desired variable type in parentheses in front of the value that you want to convert from:

 
double myDouble = 12.3456;
int myInt = (int)myDouble;     // Manual casting of converting a double into an int

Console.WriteLine(myDouble);   // outputs 12.3456
Console.WriteLine(myInt);      // outputs 12

Type Conversion Methods

Explicit casting using the Type Conversion methods is performed by using the built-in type conversion methods available with the System.Convert class:

 
int myInt = 1;
double myDouble = 6.78;
bool myBool = true;
string myString = "24.13";

Console.WriteLine(Convert.ToString(myInt));     // convert int to string
Console.WriteLine(Convert.ToDouble(myInt));     // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble));   // convert double to int
Console.WriteLine(Convert.ToString(myDouble));  // convert double to string
Console.WriteLine(Convert.ToString(myBool));    // convert bool to string
Console.WriteLine(Convert.ToInt32(myBool));     // convert bool to int
Console.WriteLine(Convert.ToInt32(myString));   // convert string to int
Console.WriteLine(Convert.ToDouble(myString));  // convert string to double

int myInt2 = Convert.ToInt32(myDouble);         // convert double to int
double myDouble2 = Convert.ToDouble(myString);  // convert string to double
bool myBool2 = Convert.ToBool(myInt);           // convert int to bool
string myString2 = Convert.ToString(myDouble);  // convert double to string


Final Thoughts

Thank you for reading, I hope you found this blog post (tutorial) educational and helpful.

 
(1)   (0)

grayStargrayStargrayStargrayStargrayStar  Be the first to rate this post

User Comments





TJDivisions© 2024