类型转换包括显示转换和隐式转换,在.NET中类型转换的基本规则如下:
- 任何类型都可以安全的转换为其基类类型,可以由隐式转换来完成;
- 任何类型转换为其派生类型时,必须进行显示转换,转换的规则是:(类型名)对象名;
- 使用GetType可以取得任何对象的精确类型;
- 基本类型可以使用Covert类实现类型转换;
- 除了string以外的其他类型都有Parse方法,用于将字符串类型转换为对应的基本类型;
- 值类型和引用类型的转换机制称为装箱(boxing)和拆箱(unboxing)。
is的规则如下:
- 检查对象类型的兼容性,并返回结果,true或者false;
- 不会抛出异常;
- 如果对象为null,则返回值永远为false。
as的规则如下:
- 检查对象类型的兼容性,并返回结果,如果不兼容就返回null;
- 不会抛出异常;
- 如果结果判断为空,则强制执行类型转换将抛出NullReferenceException异常。
纵上比较,is/as操作符,提供了更加灵活的类型转型方式,但是as操作符在执行效率上更胜一筹,我们在实际的编程中应该体会其异同,酌情量才。
为什么as 比 is 的执行效率更好?
对比两种方式,is需要做两次对象的类型检查,而as需要做一次对象类型检查,再加一次null的检查,而null检查开销比对象类型检查少。相对as的方法效率高些。
测试代码:
1
/*
2 * Created by BpLoveGcy.cnblogs.com
3 * Gump Yin
4 * Date: 2010-3-28
5 * Time: 12:03
6 *
7 * Version:
8 * CopyRight: http://BpLoveGcy.cnblogs.com/
9 */
10
11 using System;
12 using NUnit.Framework;
13
14 namespace MustKnownDotNet
15 {
16 class A
17 {
18 public virtual void PrintMe()
19 {
20 Console.WriteLine( " I'm A! " );
21 }
22 }
23 class B : A
24 {
25 public override void PrintMe()
26 {
27 Console.WriteLine( " I'm B! " );
28 }
29 }
30 class C : A
31 {
32 public override void PrintMe()
33 {
34 Console.WriteLine( " I'm C! " );
35 }
36 }
37 [TestFixture]
38 public class IsAndAs_Test
39 {
40
41 [Test]
42 public void Is_Test()
43 {
44 // TODO: Add your test.
45
46 A a = new A();
47 B b = new B();
48
49 C c = new C();
50
51 Assert.IsTrue(b is A);
52 Assert.IsTrue(c is A);
53 Assert.IsFalse(c is B);
54 Assert.IsFalse(b is C);
55 Assert.IsFalse( null is A);
56
57 if (b is A)
58 {
59 ((A)b).PrintMe();
60 }
61 if (c is A)
62 {
63 ((A)c).PrintMe();
64 }
65 }
66
67 [Test]
68 public void As_Test1()
69 {
70 A a = new A();
71 B b = new B();
72 C c = new C();
73
74 A a1 = b as A;
75 Assert.IsNotNull(a1);
76 a1.PrintMe();
77
78 A a2 = c as A;
79 Assert.IsNotNull(a2);
80 a2.PrintMe();
81
82
83 }
84 [Test]
85 public void As_Test2()
86 {
87 object o = new Object();
88 B b = o as B;
89 Assert.IsNull(b);
90 }
91 [Test]
92 [ExpectedException( typeof (NullReferenceException))]
93 public void As_Test3()
94 {
95 object o = new Object();
96 B b = o as B;
97 b.PrintMe();
98 }
99 [Test]
100 public void As_Test4()
101 {
102 object o = new Object();
103 B b = o as B;
104 if (b != null )
105 b.PrintMe();
106 }
107
108 }
109 }
110
2 * Created by BpLoveGcy.cnblogs.com
3 * Gump Yin
4 * Date: 2010-3-28
5 * Time: 12:03
6 *
7 * Version:
8 * CopyRight: http://BpLoveGcy.cnblogs.com/
9 */
10
11 using System;
12 using NUnit.Framework;
13
14 namespace MustKnownDotNet
15 {
16 class A
17 {
18 public virtual void PrintMe()
19 {
20 Console.WriteLine( " I'm A! " );
21 }
22 }
23 class B : A
24 {
25 public override void PrintMe()
26 {
27 Console.WriteLine( " I'm B! " );
28 }
29 }
30 class C : A
31 {
32 public override void PrintMe()
33 {
34 Console.WriteLine( " I'm C! " );
35 }
36 }
37 [TestFixture]
38 public class IsAndAs_Test
39 {
40
41 [Test]
42 public void Is_Test()
43 {
44 // TODO: Add your test.
45
46 A a = new A();
47 B b = new B();
48
49 C c = new C();
50
51 Assert.IsTrue(b is A);
52 Assert.IsTrue(c is A);
53 Assert.IsFalse(c is B);
54 Assert.IsFalse(b is C);
55 Assert.IsFalse( null is A);
56
57 if (b is A)
58 {
59 ((A)b).PrintMe();
60 }
61 if (c is A)
62 {
63 ((A)c).PrintMe();
64 }
65 }
66
67 [Test]
68 public void As_Test1()
69 {
70 A a = new A();
71 B b = new B();
72 C c = new C();
73
74 A a1 = b as A;
75 Assert.IsNotNull(a1);
76 a1.PrintMe();
77
78 A a2 = c as A;
79 Assert.IsNotNull(a2);
80 a2.PrintMe();
81
82
83 }
84 [Test]
85 public void As_Test2()
86 {
87 object o = new Object();
88 B b = o as B;
89 Assert.IsNull(b);
90 }
91 [Test]
92 [ExpectedException( typeof (NullReferenceException))]
93 public void As_Test3()
94 {
95 object o = new Object();
96 B b = o as B;
97 b.PrintMe();
98 }
99 [Test]
100 public void As_Test4()
101 {
102 object o = new Object();
103 B b = o as B;
104 if (b != null )
105 b.PrintMe();
106 }
107
108 }
109 }
110
ref:http://space.itpub.net/12639172/viewspace-503457
ref: http://www.cnblogs.com/anytao/archive/2007/04/07/must_net_01.html