Converting Datatype
We can convert one datatype to another using some internally available functions. Most probably this option is used to convert string to some other datatypes. This concept of converting from one datatype to another is known as Type Conversion. Below are the some methods used to convert from one datatype to another.
Method Name | Description |
---|---|
ToBoolean | Converts a type to a Boolean value, where possible. |
ToByte | Converts a type to a byte. |
ToChar | Converts a type to a character |
ToDateTime | Converts a type (integer or string type) to date-time structures. |
ToDecimal | Converts a floating point or integer type to a decimal type. |
ToDouble | Converts a type to a double type. |
ToInt16 | Converts a type to a 16-bit integer(short). |
ToInt32 | Converts a type to a 32-bit integer(int). |
ToInt64 | Converts a type to a 64-bit integer(long). |
ToSbyte | Converts a type to a signed byte type. |
ToSingle | Converts a type to a small floating point number. |
ToString | Converts a type to a string. |
ToUInt16 | Converts a type to an unsigned int type(ushort). |
ToUInt32 | Converts a type to an unsigned long type(uint). |
ToUInt64 | Converts a type to an unsigned big integer(ulong). |
CODE
static void Main(string[] args) {
//Converting string to int datatype and storing inside a int variable
string numberText = "23";
int intNumber = Convert.ToInt32(numberText);
//Converting string to double datatype and storing inside a double variable
string decimalText = "23.5466";
double decimalNumber = Convert.ToDouble(decimalText);
//Converting string to bool datatype and storing inside a bool variable
string boolText = "true";
bool boolValue = Convert.ToBoolean(boolText);
//Converting string to char datatype and storing inside a char variable
string singleCharacter = "$";
char charValue = Convert.ToChar(singleCharacter);
}
(Click Here, For more exercises)