2009/04/28

is & as

 is : 
   test t1 = new test();   /// new test 物件給t1
   object o = t1;               /// object o 參考t1的位址(heap)
   if (o is test)                 ///  判斷 o 是否為test 型別
   {
  test t2 = (test)o;   /// 轉型
   }
   
as : 
   test t1 = new test();  /// new test 物件給t1
   object o = t1;              /// object o 參考t1的位址(heap)
   test t2 = o as test;     /// 嘗試將o 轉型為 test ,若成功則t2則以參考到物件 o , 不成功則t2= null
   if (t2 !=null) 


沒有留言: