关于C#的类型转换

    前段时间宁波社区讨论了类型转换是用as关键字好还是用()好的问题,当时我轻易地相信了一片转载的文章的观点(http://hi.baidu.com/salangane0512/blog/item/24a7448b49011d17c8fc7aa9.html),认为用as比用()好。今天偶尔试验了一下,其实事情不是那样的,我当时的不严谨导致犯了一个小小的经验主义错误。

    看下面的代码:
   
    
    
//类型定义
    public interface I
    {
        
string Name { getset; }
    }

    
public class A : I
    {
        
public string Name { getset; }
    }

    
public class B : A
    {
        
public int Key;


        
public static explicit operator string(B operand)
        {
            
return operand.Name;
        }
    }


    
    
//方法调用
     private void button1_Click(object sender, EventArgs e)
    {
        B b 
= new B();
        A a1, a2;

        a1 
= (A)b;
        a2 
= b as A;

        
string h = (string)b;
    }

    对应调用方法的IL代码:
.method private hidebysig instance void button1_Click(object sender, class [mscorlib]System.EventArgs e) cil managed
{
    .maxstack 
1
    .locals init (
        [
0class WindowsFormsApplication15.B b,
        [
1class WindowsFormsApplication15.A a1,
        [
2class WindowsFormsApplication15.A a2,
        [
3string h)
    L_0000: nop 
    L_0001: newobj instance 
void WindowsFormsApplication15.B::.ctor()
    L_0006: stloc.
0 
    L_0007: ldloc.
0 
    L_0008: stloc.
1 
    L_0009: ldloc.
0 
    L_000a: stloc.
2 
    L_000b: ldloc.
0 
    L_000c: call 
string WindowsFormsApplication15.B::op_Explicit(class WindowsFormsApplication15.B)
    L_0011: stloc.
3 
    L_0012: ret 
}


 

    这下很清楚了,()和as在做类型转换的时候都是一样处理的,根本不存在所谓那个性能好那个性能坏的问题,完全是受个人习惯影响的。

    使用as唯一的好处是可以分清你是在做基于基类和接口的类型转换还是使用操作符重载机制(CLR编译器层处理)的类型转换(后一种本质上就是调用一种方法!!)。比如例子中的: string h = (string) b; 。这是使用as替代()操作符的唯一好处,而不是那篇文章中长篇大论讲的那样。

    判断类型用is,转换的话最好用as,只是因为这样清楚一些。

    大家在工作交流中经常会犯一些不严谨的错误,我在此先作自我批评,希望大家平时多加注意和改正。
原文地址:https://www.cnblogs.com/WYB/p/1236200.html