checked、is、as、sizeof、typeof运算符、空合并运算符
//--------------------------------checked防止溢出
byte b = 255;
checked
{
b++;
}
Console.WriteLine(b.ToString());//byte类型只包含0~255,加上checked所以会抛出异常
//is运算符,检查对象是否与特定的类型兼容
int i = 0;
Console.WriteLine(i is object);//True
//--------------------------------as运算符,显示转换特定的类型
object o = "123";
string s = o as string;
Console.WriteLine(s);
//--------------------------------sizeof运算符,可以确定栈中值类型的长度
Console.WriteLine(sizeof(int));//输出4
//--------------------------------typeof运算符,返回一个特定类型的System.Type对象
Console.WriteLine(typeof(string));//输出System.String
Console.ReadKey();
//--------------------------------可空运算符与空合并运算符
int? i = null;//int?可空运算符
int? a = i ?? 10;//空合并运算符 (如果??前面是空则等于第二个值)
Console.WriteLine(a);//输出10
Console.ReadKey();
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。