is(检查对象是否与给定类型兼容 )
checked/unchecked(对整型算术运算和显式转换启用溢出检查 ,否则算术运算回绕)
还记得我们前面对byte的一个转换吧,再看看下面,你就有更深的理解
1 object o = new object();
2 o = "hello";
3 System.Console.WriteLine("\"hello\"-string->{0}", o is string);
4 o = 123;
5 System.Console.WriteLine("123-string->{0}", o is string);
6 System.Console.WriteLine("123-String->{0}", o is String);
7 System.Console.WriteLine("123-int->{0}", o is int);
8 System.Console.WriteLine("123-Int32->{0}", o is Int32);
9 System.Console.WriteLine("123-Int16->{0}", o is Int16);
10 DateTime d = DateTime.Now;
11 System.Console.WriteLine("Now-DateTime->{0}", d is DateTime);
12 string dd = "2006-01-01";
13 System.Console.WriteLine("\"2006-01-01\"-DateTime->{0}", dd is DateTime);
2 o = "hello";
3 System.Console.WriteLine("\"hello\"-string->{0}", o is string);
4 o = 123;
5 System.Console.WriteLine("123-string->{0}", o is string);
6 System.Console.WriteLine("123-String->{0}", o is String);
7 System.Console.WriteLine("123-int->{0}", o is int);
8 System.Console.WriteLine("123-Int32->{0}", o is Int32);
9 System.Console.WriteLine("123-Int16->{0}", o is Int16);
10 DateTime d = DateTime.Now;
11 System.Console.WriteLine("Now-DateTime->{0}", d is DateTime);
12 string dd = "2006-01-01";
13 System.Console.WriteLine("\"2006-01-01\"-DateTime->{0}", dd is DateTime);
as(用于在兼容的引用类型之间执行转换 )
1 //as只用于引用类型
2 object o = new object();
3 o = "hello";
4 System.Console.WriteLine(o as string);
5 o = 123;
6 System.Console.WriteLine(o as string);
7 System.Console.WriteLine(o as String);
8 //System.Console.WriteLine(o as int); as对值类型无效
9 //System.Console.WriteLine(o as Int32); as对值类型无效
10 //System.Console.WriteLine(o as Int16); as对值类型无效
11 //DateTime d = DateTime.Now; as对值类型无效
12 //System.Console.WriteLine(d as DateTime); as对值类型无效
13 //string dd = "2006-01-01"; as对值类型无效
14 //System.Console.WriteLine(dd as DateTime); as对值类型无效
2 object o = new object();
3 o = "hello";
4 System.Console.WriteLine(o as string);
5 o = 123;
6 System.Console.WriteLine(o as string);
7 System.Console.WriteLine(o as String);
8 //System.Console.WriteLine(o as int); as对值类型无效
9 //System.Console.WriteLine(o as Int32); as对值类型无效
10 //System.Console.WriteLine(o as Int16); as对值类型无效
11 //DateTime d = DateTime.Now; as对值类型无效
12 //System.Console.WriteLine(d as DateTime); as对值类型无效
13 //string dd = "2006-01-01"; as对值类型无效
14 //System.Console.WriteLine(dd as DateTime); as对值类型无效
checked/unchecked(对整型算术运算和显式转换启用溢出检查 ,否则算术运算回绕)
还记得我们前面对byte的一个转换吧,再看看下面,你就有更深的理解
1 byte b = 100;
2 System.Console.WriteLine((byte)(b + 200));//不抛出异常
3 System.Console.WriteLine(checked((byte)(b + 200)));//抛出异常
4 System.Console.WriteLine(unchecked((byte)(b + 200)));//不抛出异常
2 System.Console.WriteLine((byte)(b + 200));//不抛出异常
3 System.Console.WriteLine(checked((byte)(b + 200)));//抛出异常
4 System.Console.WriteLine(unchecked((byte)(b + 200)));//不抛出异常