2.表达式:
常见的表达式及运算符
static void Main()


{
int result = 0;//=为赋值运算符,这是一个赋值表达式.
int number;
result = number * 100;//这是一个表达式,=为赋值运算符,*为算术运算符
Console.WriteLine(result);//这里的.号就指成员访问运算符
}
一元运算符(++/--)及表达式
static void Main()

{
int inti = 0;//声明变量
int intj = 0;
for (int i = 0; i < 10; i++)

{
inti++;
intj--;
}
Console.WriteLine("inti++运算后结果为{0},intj--运算后结果为{1}", inti, intj);
}
//运行结果为inti=10,intj=-10
由此可见,inti++相当于inti=inti+1,而intj--相当于intj=intj-1.
三元运算符及表达式
static void Main()

{
int inti = 1;//声明变量
string sex = inti.ToString()+" ";
sex += (inti == 1 ? "男人" : "女人");//如果inti为1 时候sex为男人,否则的话sex为女人
Console.WriteLine(sex);
}
三元运算符还是常用滴.希望能多练练.
is操作符:
is操作符是用来动态的检测运行时对像类型是否与指定类型兼容,运算结果返回一个bool值,例:
public static void Main()

{
Console.WriteLine(1 is int);
Console.WriteLine(1 is float);
Console.WriteLine(1.0 is float);
Console.WriteLine(1.0 is double);
}
以上代码的返回结果为:true false false true
需要注意的是,我们能表述的意思是 苹果是水果,得到的答案为true.
as操作符:
as操作符应用于兼容的引用类型之间的转换.返回值为值本身或null.如:
public static void Main()

{
Console.WriteLine(“a” as string);
}
以上内容基本叙述了C#中的运算符及表达式,如果无法明白或有错误的地方,请回复指证,谢谢大家.