C Sharp Type Casting - chrisbitm/python GitHub Wiki
In C#, type casting is the process of converting a value from one data type to another. There are two methods: Implicit (Automatic) and Explicit (Manual)
Implicit Casting
Implicit casting is done automatically when passing a smaller size type to a larger size type (Example: int
to long
)
int num = 123;
double bigNum = num;
Explicit Casting
This is done by passing a Larger Data type to a Smaller Data Type (Example: double
to int
)
double myDouble = 9.78;
int myInt = (int) myDouble;
Console.WriteLine(myDouble);
Console.WriteLine(myInt);
- You must put wrap Parenthesis
()
in front of a Value to cast the desired Data Type.
Bult-in Methods
This converts a Number to a String, a Boolean to a String, etc.
int myInt = 36;
double myDouble = 4.87;
bool myBool = false;
Console.WriteLine(Convert.ToString(myInt));
Console.WriteLine(Convert.ToDouble(myInt));
Console.WriteLine(Convert.ToInt32(myDouble));
Console.WriteLine(Convert.ToString(myBool));