一.Parse()适用于string转换成值类型
string strNumber = "123";
int intNumber4= int.Parse(strNumber);
Console.WriteLine(intNumber4);
long longNumber4 =long.Parse(strNumber);
Console.WriteLine(longNumber4);
byte byteNumber =byte.Parse(strNumber);
Console.WriteLine(byteNumber);
uint uintNumber =uint.Parse(strNumber);
Console.WriteLine(uintNumber);
float floatNumber4 = float.Parse(strNumber);
Console.WriteLine(floatNumber4);
// 注意bool类型转换
string stringNumber5 = "true";
bool boolNumber = bool.Parse(stringNumber5);
Console.WriteLine(boolNumber);
1. int.TryParse
int.TryParse与 int.Parse 又较为类似,但它不会产生异常,转换成功返回 true,
转换失败返回 false。最后一个参数为输出值,如果转换失败,输出值为 0
//尝试转换数据 如果不成功 会返回false结果
//成功返回 true
int ab; //用于输出参数
string stringNumber6 = "a12";
bool isSuccess = int.TryParse(stringNumber6, out ab);
string isSuccessStr = isSuccess?"转换成功":"转换失败";
Console.WriteLine(isSuccessStr);
Console.WriteLine(ab); // 转换失败结果为 0
2.Convert类型转换
适合各种类型相互转换
//万能类型转换 任意类型之间转换 但是转换的内容要符合转换后类型的标准
//否则抛出异常
string stringNumber7 = "123";
int intNumber7 = Convert.ToInt32(stringNumber7);
long longNumber7 =Convert.ToInt64 (stringNumber7);
short shortNumber7 = Convert.ToInt16 (stringNumber7);
uint uintNumber7 = Convert.ToUInt32 (stringNumber7);
ulong ulongNumber7 = Convert.ToUInt64 (stringNumber7);
ushort ushortNumber7 = Convert.ToUInt16 (stringNumber7);
float floatNumber7 = Convert.ToSingle(stringNumber7);
double doubleNumber7 = Convert.ToDouble(stringNumber7);
//bool类型转换成数字类型 时 对应的是 0 和1
bool boolNumber7 = true;
int intNumber8 = Convert.ToInt32(boolNumber7);
Console.WriteLine(intNumber8);
//这种情况就无法转换
string s1 = "123abc";
int a1 = Convert.ToInt32(s);
double b1 = Convert.ToDouble(s);
3.ToString()
适合其他类型转换成字符串.
//其他类型转换成字符串类型 方式1
int intNumber9 = 100;
Console.WriteLine(Convert.ToString(intNumber9));
//其他类型转换成字符串类型 方式2
string strNumber9 = intNumber9.ToString();
Console.WriteLine(strNumber9);
4.算数运算符
+ - * / % ++ --
//算术运算符
// + - * / %数字类型大类型和小类型结合 结果默认为大类型结果
int intNumber = 10;
int intNumber1 = 40;
long longNumber = 20;
//默认类型int
int intNumber3 = intNumber + intNumber1;
//隐式类型转换
long longNumber3 = intNumber + intNumber1;
//显示类型转换
int intNumber4 = (int)(intNumber + longNumber);
int intNumber5 = 100;
float floatNumber = 10f;
//字符串类型和字符串类型相加 拼接
string stringNumber = "123";
string stringNumber1 = "abc";
string stringNumber2 = stringNumber + stringNumber1;
//字符串类型和数字类型相加 拼接
string stringNumber3 = "123";
int intNumber6 = 10;
Console.WriteLine(stringNumber3 + intNumber6);
//字符串类型和数字类型其他操作 都是不允许的
//Console.WriteLine(stringNumber3 - intNumber6);
//Console.WriteLine(stringNumber3 * intNumber6);
//Console.WriteLine(stringNumber3 / intNumber6);
//Console.WriteLine(stringNumber3 % intNumber6);
//单字符和数字类型
//单字符ascii表对应的10进制数字和数字类型进行数学运算
char charNumber = 'a';
int intNumber7 = 10;
//得到对应的单字符ASCII值
int charNumber1 = (int)charNumber;
Console.WriteLine(charNumber1);
Console.WriteLine(charNumber + intNumber7);
Console.WriteLine(charNumber - intNumber7);
Console.WriteLine(charNumber * intNumber7);
Console.WriteLine(charNumber / intNumber7);
Console.WriteLine(charNumber % intNumber7);
//++ --
int intNumber9 = 10;
intNumber9++;
++intNumber9;
// 12 14 13 13
int SumIntNumber = (intNumber9++) + (++intNumber9) - (--intNumber9) + (intNumber9--);
//26
Console.WriteLine(SumIntNumber);
//12
Console.WriteLine(intNumber9);
Console.ReadKey();
5.关系运算符
> < >= <= == !=
// 关系运算符
int intNumber10 = 10;
int intNumber11 = 20;
bool isTrue = intNumber10 < intNumber11;
Console.WriteLine(isTrue);
string stringNumber10 = "10";
//字符串无法和数字类型比较
//bool isTrue1 = intNumber10 < stringNumber10;
char charNumber10 = 'a';
bool isTrue2 =intNumber10 < charNumber10;
Console.WriteLine(isTrue2);
// > >= <= 同上
// == !=
int intNumber12 = 100;
int intNumber13 = 200;
bool isTrue3 = intNumber12 ==intNumber13;
bool isTrue4 = intNumber12 != intNumber13;
//字符串类型之间 == != 都能使用
string stringNumber12 = "123";
string stringNumber13 = "abc";
char charNumber12 = 'b';
bool isTrue5 = stringNumber12 == stringNumber13;
bool isTrue6 = stringNumber12 != stringNumber13;
bool isTrue7 = charNumber12 == 97;
//sizeof() 返回数据类型的大小。
Console.WriteLine(sizeof(int));
Console.WriteLine(sizeof(long));
//判断数据类型方式 GetType()
int intNumber16 = 10;
long longNumber17 = 20;
Console.WriteLine(longNumber17.GetType());
Console.WriteLine(intNumber16.GetType());