(A) Type Conversion Convert
- Compatible type two variables, automatic type conversions may be used, or cast;
- When two variable types are not compatible when using the Convert
- In fact Convert the ground floor, in converted to int when it is int.Parse (need to convert content) calls; so a direct call int.Parse () more efficient
- Each type has a type .Parse ()
//int.TryParse()
bool b = int.TryParse("123", out number);
//这个句子,尝试将一个字符串转换为int,转换成功,则赋值给number,返回true;转换失败,number赋值为0,返回false;
//并不会抛出异常
//使用Convert,下面有很多转换类型
double d = Convert.ToDouble("123.456789");
Console.WriteLine("这个数字是{0:0.00}", d);
//输出123.46(数字Double类型);在占位符中使用冒号
*** NOTE: *** use the Convert time, can be converted to common sense, such as "123ABC", can not be converted to int32;
(Ii) simple calculations
int i = 10;
Console.WriteLine(i++);//输出10
int i = 10;
Console.WriteLine(++i);//输出11
//符号i--与--i类似
*** NOTE: *** unary operator has higher precedence than a binary operator; unary operators that only operand;
(Iii) relational operators, and logical operators
- Relational operators>, <,> =, <=, ==,! =;
- bool type described right or wrong, only two values true, false
The results relational expression is of type bool;
- &&, ||,! Three logical expression
The value of the logical operator expression or relationship on both sides are generally of the type bool ; higher priority than logical AND or logical, but are preferably small brackets;
(D) if, if-else branch structure
- Sequence Structure
- Branch structure: if, if-else
- Selection structure: if-else-if, switch-case
- Loop structure: while, do-while, for, foreach
//语法
if(判断条件)
{
要执行的代码;//条件成立时
}
else
{
要执行的代码;//条件不成立时
}
//先判断再执行;这是一个分支结构,判断所执行的分支;
//处理多条件区间性的判断
if(判断条件)
{
执行语句;//条件成立
}
else if(条件判断)
{
执行语句;//条件成立
}
else if(条件判断)
{
执行语句;//条件成立
}
else if(条件判断)
{
执行语句;//条件成立
}