I haven't read Don's book, but I think he's comparing:
If (obj is Employee)
{
Employee e = (Employee) obj;
}
To
Employee e = obj as Employee;
If (e != null)
{
//...
}
In the first case, there are two tests (on at the is, and one on the
cast), and on the second, only a single test.
-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Jekke Bladt
Sent: Wednesday, September 03, 2003 1:13 PM
To: [EMAIL PROTECTED]
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
==
"Of course we're going to be, as a movement, blowing things up and
smashing windows...I think it's perfectly appropriate for people to take
bricks and toss them through windows ... Hallelujah to the people who
are willing to do it."
--PETA Campaign Director Bruce Friedrich to wild applause from fellow
would-be eco-terrorists.
===================================
This list is hosted by DevelopMentor(r) 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
===================================
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