The as keyword only works on Reference types.
So, this is illegal:
int x = obj as int;

Personally I don't see why there's more need for structured errorhandling
when you do this:
if (obj != null && obj is SomeType)
    ((SomeType) obj).Method();

instead of this:
SomeType st = obj as SomeType;
if (st != null)
    st.Method();


Regards,

Peter
----- Original Message -----
From: "Jekke Bladt" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, September 03, 2003 10:12 PM
Subject: [ADVANCED-DOTNET] Advantages to castclass opcode over isinst


> All--
>
> I'm ploughing through Don Box's CLR book and have come to part that leaves
> me puzzled:
>
> Box writes that the castclass opcode (generated by code like (Type)Object)
> is far more computationally expensive than isinst (generated by obj1 =
obj2
> As Type) because of the potential overhead of invoking a structured error.
> He then goes on to say that it's therefore preferrable to use ...As Type
> style casting when you know at design time that a cast is possible and
> (Type)Object casting when a runtime error is possible.
>
> I don't understand the second part of the trade-off. If ...As Type style
> casting can be tested for using the "is" keyword and castclass is so
> process-intensive, why not always use "As" style casting?
>
> TIA
>
> --Jekke

===================================
This list is hosted by DevelopMentor®  http://www.develop.com
NEW! ASP.NET courses you may be interested in:

2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet

Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet

View archives and manage your subscription(s) at http://discuss.develop.com
  • [A... אוהד ישראלי
    • ... Jekke Bladt
      • ... Peter Vervoorn
    • ... J. Merrill
    • ... אוהד ישראלי
    • ... Paul Stevens
    • ... אוהד ישראלי
    • ... Paul Stevens
    • ... J. Merrill

Reply via email to