当前位置:IT168首页 > 技术开发 > 概述
[收藏此页] [打印] [推荐] [评论]

关于C#的类型转换

责任编辑:nancy作者:ITPUB论坛   2008-07-07   
文本Tag: 微软 sql

【IT168技术文档】

  看下面的代码:
//类型定义 public interface I { string Name { get; set; } } public class A : I { public string Name { get; set; } } 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 ( [0] class WindowsFormsApplication15.B b, [1] class WindowsFormsApplication15.A a1, [2] class WindowsFormsApplication15.A a2, [3] string 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在做类型转换的时候都是一样处理的,根本不存在所谓那个性能好那个性能坏的问题,完全是受个人习惯影响的。
上一页
1
下一页
收藏到: 添加到“百度搜藏”添加到“QQ书签”添加到“Google书签”添加到“Yahoo收藏”添加到“和讯网摘”
【内容导航】
本文欢迎转载,转载请注明:转载自IT168 [ http://www.it168.com/ ]
本文链接:http://tech.it168.com/d/2008-07-07/200807071517122.shtml
技术开发相关文章  
  • 暂无
友情推介