12月
30
C#中的数据类型转换
- String转换成其他数据类型
string strNumber = "18";
int num1 = int.Parse(strNumber);
float num2 = float.Parse(strNumber);
- ToString转换:任意类型转换为String类型
int number = 18;
string str = number.ToString();
- 隐式转换:自动转换
byte b = 100;
int i = b;
- 显式转换:强制转换
int i = 100;
byte b = (byte)i;
- 多种类型变量参与的运算,会产生类型提升
其中的一方为long,则结果为long
其中的一方为float,则结果为float
其中的一方为double,则结果为double
其中的一方为decimal,则结果为decimal
其余运算结果为int
注意,快捷运算符(例如+=),不会产生类型提升
(未完待续)