1.测试运算符:is
is操作符作用于动态地检查表达式是否为指定类型。使用格式为:e is T,其中e是一个表达式,T是一个类型,返回值是一个布尔值。
if(1 is int){ //true
......
}
while(1 is float){ //false
.....
}
while(1.0f is float){ //true
.....
}
2.typeof运算符
typeof用于获得指定类型在system命名空间中定义的类型名字,例:
using System;
class Test{
static void Main(string[] args){
Console.WriteLine(typeof(int)); //System.Int32
Console.WriteLine(typeof(System.Int32)); //System.Int32
Console.WriteLine(typeof(string)); //System.String
Console.WriteLine(typeof(double[])); //System.Double[]
}
}
3.溢出检查操作符checked和unchecked
在进行整型算数运算或从一种整形显式转化到另一种整型时,有可能出现运算结果超出类型值域的情况,称为溢出。
如果表达式不使用溢出检查操作符或使用了checked操作符,若溢出了,编译时将产生错误,运行时将产生异常提示信息。而使用unchecked操作符的表达式语句,即使产生了溢出,编译和运行时都不会产生错误提示信息。
using System;
class Test{
static void Main(string[] args){
const int x=int.MaxValue;
unchecked{
int z=x*2; //编译时不产生编译错误,z=-2;
Console.WriteLine("z={0}",z); //显示-2
}
checked{
int z1=(2*x); //编译时产生错误。
}
}
}
4.new运算符:
可以用来创建值类型变量、引用类型对象,同时自动调用构造函数:
int y=new int();
Person p=new Person();
int[] arr=new int[2];