运算符的介绍

/*
运算符的介绍
*/
using System;

namespace Frank
{
	public class Test
    {
        public static void Main(string[] args)
        {
			//检查运算
			byte b = 255;
			checked //执行安全检查
			{
				//b++;//异常
			}
			unchecked//不执行安全检查,默认方式
			{
				b++;//不异常,但是溢出会得到0
			}
			System.Console.WriteLine(b);

			//is运算
			int i = 10;
			if(i is string)//判断i是否是string类型
			{
				System.Console.WriteLine(i);
			}

			//as运算符 只能用于引用类型,不能用于值类型
			object ok = "1";
			string str1 = ok as string;//把ok转换成stirng类型
			object ok2 = 5;
			string str2 = ok2 as string;//如果成功返回string类型值,不成功则返回null
			string str3 = "s";
			//int i1 = str3 as int;
			System.Console.WriteLine(str1+"---"+str2);

			//sizeof运算符
			Console.WriteLine("int类型的大小:"+sizeof(int));//返回4个字节
			
			//typeof运算符 返回System.Type类型
			System.Console.WriteLine("int的System.Type类型:"+typeof(int));

			//可null类型的基类 bool不能赋值空,bool类型不是一个可空类型
			int? i2 = null;
			double? d1 = null;
			char? c1= null;
			if(i2 == d1)
			{
				System.Console.WriteLine(i2 == d1);
			}

			//??空合并符
			int? i3 = null;
			int i4 = i3 ?? 0;//为空就是后面的默认值
			System.Console.WriteLine(i4);
        }
	}
}

  

posted on 2013-11-27 15:45  wp456  阅读(165)  评论(0)    收藏  举报

导航