SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
Generation 5 » Prefix-casting versus as-casting in C#

 Subscribe to our RSS Feed | About Us

Prefix-casting versus as-casting in C#
Introduction
This is a story of two types: GenericType and SpecificType, where GenericType is a
superclass of SpecificType. There are two types of explicit cast in C#:
The Prefix cast:
[01] GenericType g=...;
[02] SpecificType t=(SpecificType) g;

The as cast:
[03] GenericType g=...;
[04] SpecificType t=g as SpecificType;

Most programmers have a habit of using one or the other — this isn’t usually a
conscious decision, but more of a function of which form a programmer saw first. I,
for instance, programmed in Java before I learned C#, so I was already in the prefix
cast habit. People with a Visual Basic background often do the opposite. There are
real differences between the two casting operators which can make a difference in the
reliability and performance of your application.

Prefix casting: Reliable Casting
The major difference between prefix- and as-casting is what happens when the cast
fails. Imagine, for instance, that g is really an instance of AnotherSpecificType, which
is not a subclass of SpecificType. In this case, the prefix-cast throws an exception at
line [2] — however, the as-cast returns null when the cast fails, letting the execution
of the program barrel on.
It’s easier to implement correct error handling in programs that use prefix-casting,
and programs that use prefix-casting are easier to debug. Prefix-casting causes an
exception to be thrown at the moment of the cast, where the problem is obvious. Ascasting, on the other hand, can cause an exception to be thrown at the moment
where the SpecificType t is referenced. The used of the SpecificType can be far away
in the program: it can be in another method, or another class — it could even happen
hours after the cast is performed. Be it in development or production, bugs caused by
corrupted data structures can be maddeningly difficult to find and correct.

As-casting: Fast Casting
If it’s harder to write correct programs with as-casting, why would anybody use it? For
one thing, as-casting is faster than prefix casting by quite a lot. Benchmarks show
that as-casting is about five times faster than prefix casting. That said, the
performance of casting is only going to matter in the innermost of loops of most
programs. The fastest kind of casting is no casting at all: it’s best to use generics to
eliminate the need for casting when possible and to move casting outside loops.
(Generics also improve the reliability of your code because they help C#’s static type
checking catch mistakes.)
There are some cases where as-casting could be convenient, for instance, when you
expect the cast to fail sometimes. Often I like to ‘tag’ classes with interfaces that
specify certain behaviors. For example,
[05] public Interface IBoppable {
[06]
void Bop();
[07] }

Now i might want to loop through a bunch of Thing objects, and bop all the ones that
implement IBoppable: it’s reasonable to use as-casting here:
[08] List<Thing> list=...
[09] foreach(Thing thing in list) {

http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/[1/16/2014 3:55:16 PM]

Search for:
Search

Archives

June 2012 (1)
August 2010 (1)
May 2010 (1)
June 2009 (2)
April 2009 (1)
March 2009 (1)
February 2009 (3)
January 2009 (3)
November 2008 (1)
August 2008 (2)
July 2008 (5)
June 2008 (5)
May 2008 (2)
April 2008 (6)
March 2008 (8)
June 2006 (1)
February 2006 (1)

Categories

AJAX (2)
Asynchronous Communications (16)
Biology (1)
Books (1)
Design (1)
Distributed (1)
Exceptions (2)
Functional Programming (1)
GIS (1)
Ithaca (1)
Japan (1)
Math (1)
Media (3)
Nature (1)
Semantic Web (3)
Tools (28)
CRUD (1)
Dot Net (17)
Freebase (2)
GWT (9)
Java (7)
Linq (2)
PHP (6)
Server Frameworks (1)
Silverlight (12)
SQL (5)
Uncategorized (1)
Web (2)
Analytics (1)
Generation 5 » Prefix-casting versus as-casting in C#

[10]
[11]
[12]
[13]
[14] }

List boppable=thing as IBoppable;
if (boppable !=null) {
boppable.Bop()
}

It’s OK to use as-casting if you’re going to check to see if the value is null immediately
after the cast. The above code is correct, but has the bad smell that the boppable
variable continues to exist in the block after the cast… It’s still there for a later
maintainer to use erroneously. In cases like this, code can be made clearer with the is
operator:
[15] List<Thing> list=...
[16] foreach(Thing thing in list) {
[17]
if(thing is IBoppable) {
[18]
((IBoppable) boppable).Bop()
[19]
}
[20] }

(Speed freaks can use as-cast on line 18, as we know it’s not going to fail.)
The pattern of testing for null after an as-cast has a few problems. (i) It doesn’t
distinguish between the case of the original object being null from the case of the
original object being the wrong type and (ii) correct error handling often requires
more contorted logic than using an exception — and once you added test logic,
you’ve lost the speed advantage of as-casting.

Conclusion
C# offers two casting operators: the prefix-cast and the as-cast. Although the two
operators compile to different op-codes in the CLR, the practical difference between
them is in how they handle failed casts. Prefix-cast throws an exception on cast
failure, while as-cast returns null . It’s easier to implement correct error handling when
you use prefix cast, because it doesn’t require manual checks for null values that can
cause problems in distant parts of your program. Prefix-cast should be the default cast
operator on your fingertips, that you use for everyday situations — reserve as-cast for
special cases where performance matters. (For best performance, however, eliminate
the cast entirely.)

Paul Houle on June 13th 2008 in Dot Net

Comments (13)

Comments (13)

Login

Sort by: Date Rating Last Activity
Michael james · 291 weeks ago

+2

Great post, i learnt something new today. I always use type cast but have colleagues that use as.
Knowing the difference will be a great help
Reply

Will · 291 weeks ago

0

I disagree. I'd say the rule of thumb is:
If you cannot continue if the cast fails, then use the prefix style cast, which will result in an exception.
If you can continue if the cast fails, then use a combination of "if object is type ... " and "as" casts.
You should NEVER do this:
MyType t;
try { t = (MyType)foo; }
catch { // do something here if cast fails and return }
// do something here if cast succeeds and return
That's a situation where "is" and "as" operators are useful. An example would be checking addins to
see if they implement IDisposable. You won't know this at compile time, so you'll have to make some
type of check.
I've found that the majority of time I can handle invalid casts more often than not, so the majority of

http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/[1/16/2014 3:55:16 PM]
Generation 5 » Prefix-casting versus as-casting in C#

time I'm using "is" and "as".
Reply

Janko · 291 weeks ago

0

Very good article!
Reply

cristian · 291 weeks ago

0

Hi there, nice post. I think you should mention or write a post about about explicit type conversion. It's
an important distinction of "prefix-casting".
Reply

Sebastian · 291 weeks ago

0

I don't think articles about performance in .Net 1.1 should be used as a reference in 2008. Just read
the article's comments or try it yourself before making such conclusions.
IF you want performance then do the _exact opposite_ of your "IBobbable" example. The first snippet
(as) will use isinst (and a branch instruction) whereas the is-cast snippet will use isinst and castclass. A
quick test shows a performance gain of about 12%, whew! This as-null pattern is the only place I use
that operator. And now guess which use of "as" can be found in the "as keyword [C#]" SDK article ;) .
Reply

Tim C · 291 weeks ago

0

"as" is faster? Sample code to back that up? In my testing it was for all intents and purposes EXACTLY
the same.
Reply

Zack Owens · 291 weeks ago

+1

I'm not quite sure there is a situation that would require one type of cast over the other when you are
dealing with classes or interfaces. Both are interchangeable syntactically and logically.
But also keep in mind that the as operator will only work for classes and interfaces. You must use the
prefix cast when working with an enum or struct. The is operator, however, will work with enums and
structs.
Reply

Maarten Oosterhoff · 291 weeks ago

0

Another difference is that value types cannot be cast using the 'as' keyword, you must us prefix-casting
for value-types.
I usually use the is/as keyword, I find that works best for me to understand the code.
Nice article though!
Reply

Petar Petrov · 291 weeks ago

0

I totally agree with Will.
I always use "as" operator over "is"+"as". However there is situations where I want to force the type so
I use the prefix cast.
Reply

Anu Viswan · 281 weeks ago
Hi
I would disagree with you on the latter part in which you mentioned "as" casting is faster.
Can you provide some code sample to support your claim ??

http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/[1/16/2014 3:55:16 PM]

0
Generation 5 » Prefix-casting versus as-casting in C#

Thanks
Anu
Reply

Paul Houle · 281 weeks ago

+2

@Anu,
according to reports I've seen, the performance of as- and prefix casts have largely converged as of
.net 3.5. It would be interesting to run tests on mono as well.
At this point, I think people should use whatever convention they think is better from a sofware
engineering standpoint. In most cases, I think it's better to get an exception early rather than face a
null pointer deference later. On the other hand, there are definitely cases where the as-cast semantics
are exactly what you want.
Reply

Anu Viswan · 281 weeks ago

0

Thanks Paul for your kind reply. I understand the speed difference is trivial and doesnt really makes too
much impact in real life application. Only thing that would matter is how each handle exception, which
again you were spot on that its better to catch an exception earlier than face null later.
I was just curious about the differnce. After i read your article, i tried out my little study on differnce
and it seems like prefix casting uses "castclass" function in the IL code generated while "as" casting
would use isinst.
Reading about these IL commands, made me wunder again.
Did you happen to check out the differnce in IL ?
Reply

BlahaK · 79 weeks ago

0

Hi, could anybody help me?
I am trying to use prefix-casting dynamicaly by this way:
' long userID = (Type.GetType("long"))mySqlDataReader.GetValue(i); '
Of course it does not work, maybe because "long" is "struct"?
.NET says "Cannot implicitly convert 'System.Type' to 'long'.
So my question is: how to do this?
Thanks for your help.
Reply

Post a new comment
Enter text right here!

Comment as a Guest, or login:
Name

Email

Website (optional)

Displayed next to your comments.

Not displayed publicly.

If you have a website, link to it here.

None
Subscribe to None

Submit Comment

Copyright © 2013 Generation 5.
WordPress Theme design.

http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/[1/16/2014 3:55:16 PM]

Weitere ähnliche Inhalte

Was ist angesagt?

TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012Pietro Di Bello
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeTerry Yin
 
PVS-Studio Has Finally Got to Boost
PVS-Studio Has Finally Got to BoostPVS-Studio Has Finally Got to Boost
PVS-Studio Has Finally Got to BoostAndrey Karpov
 
TDD Walkthrough - Encryption
TDD Walkthrough - EncryptionTDD Walkthrough - Encryption
TDD Walkthrough - EncryptionPeterKha2
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6BlackRabbitCoder
 
Why Static Type Checking is Better
Why Static Type Checking is BetterWhy Static Type Checking is Better
Why Static Type Checking is BetterAndrew Rota
 
Chaos Engineering Talk at DevOps Days Austin
Chaos Engineering Talk at DevOps Days AustinChaos Engineering Talk at DevOps Days Austin
Chaos Engineering Talk at DevOps Days Austinmatthewbrahms
 
How penetration testing techniques can help you improve your qa skills
How penetration testing techniques can help you improve your qa skillsHow penetration testing techniques can help you improve your qa skills
How penetration testing techniques can help you improve your qa skillsMarian Marinov
 
iOS Unit Testing
iOS Unit TestingiOS Unit Testing
iOS Unit Testingsgleadow
 

Was ist angesagt? (13)

TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy Code
 
PVS-Studio Has Finally Got to Boost
PVS-Studio Has Finally Got to BoostPVS-Studio Has Finally Got to Boost
PVS-Studio Has Finally Got to Boost
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
TDD Walkthrough - Encryption
TDD Walkthrough - EncryptionTDD Walkthrough - Encryption
TDD Walkthrough - Encryption
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
 
TDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and JasmineTDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and Jasmine
 
RubyTesting
RubyTestingRubyTesting
RubyTesting
 
Why Static Type Checking is Better
Why Static Type Checking is BetterWhy Static Type Checking is Better
Why Static Type Checking is Better
 
Chaos Engineering Talk at DevOps Days Austin
Chaos Engineering Talk at DevOps Days AustinChaos Engineering Talk at DevOps Days Austin
Chaos Engineering Talk at DevOps Days Austin
 
How penetration testing techniques can help you improve your qa skills
How penetration testing techniques can help you improve your qa skillsHow penetration testing techniques can help you improve your qa skills
How penetration testing techniques can help you improve your qa skills
 
IntroTestMore
IntroTestMoreIntroTestMore
IntroTestMore
 
iOS Unit Testing
iOS Unit TestingiOS Unit Testing
iOS Unit Testing
 

Ähnlich wie Prefix casting versus as-casting in c#

Dear compiler please don't be my nanny v2
Dear compiler  please don't be my nanny v2Dear compiler  please don't be my nanny v2
Dear compiler please don't be my nanny v2Dino Dini
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flexmichael.labriola
 
Grails Worst Practices
Grails Worst PracticesGrails Worst Practices
Grails Worst PracticesBurt Beckwith
 
Searching for bugs in Mono: there are hundreds of them!
Searching for bugs in Mono: there are hundreds of them!Searching for bugs in Mono: there are hundreds of them!
Searching for bugs in Mono: there are hundreds of them!PVS-Studio
 
Konstantin Knizhnik: static analysis, a view from aside
Konstantin Knizhnik: static analysis, a view from asideKonstantin Knizhnik: static analysis, a view from aside
Konstantin Knizhnik: static analysis, a view from asidePVS-Studio
 
A Pragmatic Approach
A Pragmatic ApproachA Pragmatic Approach
A Pragmatic ApproachHakanCanpek
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming WebStackAcademy
 
Realtime selenium interview questions
Realtime selenium interview questionsRealtime selenium interview questions
Realtime selenium interview questionsKuldeep Pawar
 
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...Applitools
 
c-for-c-programmers.pdf
c-for-c-programmers.pdfc-for-c-programmers.pdf
c-for-c-programmers.pdfSalar32
 
Clean code quotes - Citações e provocações
Clean code quotes - Citações e provocaçõesClean code quotes - Citações e provocações
Clean code quotes - Citações e provocaçõesAndré de Fontana Ignacio
 
Node.js meetup 17.05.2017 ember.js - escape the javascript fatigue
Node.js meetup 17.05.2017   ember.js - escape the javascript fatigueNode.js meetup 17.05.2017   ember.js - escape the javascript fatigue
Node.js meetup 17.05.2017 ember.js - escape the javascript fatigueTobias Braner
 
C# and java comparing programming languages
C# and java  comparing programming languagesC# and java  comparing programming languages
C# and java comparing programming languagesShishir Roy
 
Google mock training
Google mock trainingGoogle mock training
Google mock trainingThierry Gayet
 
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...Andrey Karpov
 
Dot Net Accenture
Dot Net AccentureDot Net Accenture
Dot Net AccentureSri K
 
Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016Matt Raible
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsSteven Smith
 
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in ActionBill Scott
 

Ähnlich wie Prefix casting versus as-casting in c# (20)

Dear compiler please don't be my nanny v2
Dear compiler  please don't be my nanny v2Dear compiler  please don't be my nanny v2
Dear compiler please don't be my nanny v2
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
Grails Worst Practices
Grails Worst PracticesGrails Worst Practices
Grails Worst Practices
 
Searching for bugs in Mono: there are hundreds of them!
Searching for bugs in Mono: there are hundreds of them!Searching for bugs in Mono: there are hundreds of them!
Searching for bugs in Mono: there are hundreds of them!
 
Konstantin Knizhnik: static analysis, a view from aside
Konstantin Knizhnik: static analysis, a view from asideKonstantin Knizhnik: static analysis, a view from aside
Konstantin Knizhnik: static analysis, a view from aside
 
A Pragmatic Approach
A Pragmatic ApproachA Pragmatic Approach
A Pragmatic Approach
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Realtime selenium interview questions
Realtime selenium interview questionsRealtime selenium interview questions
Realtime selenium interview questions
 
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...
Testing Hourglass at Jira Frontend - by Alexey Shpakov, Sr. Developer @ Atlas...
 
c-for-c-programmers.pdf
c-for-c-programmers.pdfc-for-c-programmers.pdf
c-for-c-programmers.pdf
 
Clean code quotes - Citações e provocações
Clean code quotes - Citações e provocaçõesClean code quotes - Citações e provocações
Clean code quotes - Citações e provocações
 
Node.js meetup 17.05.2017 ember.js - escape the javascript fatigue
Node.js meetup 17.05.2017   ember.js - escape the javascript fatigueNode.js meetup 17.05.2017   ember.js - escape the javascript fatigue
Node.js meetup 17.05.2017 ember.js - escape the javascript fatigue
 
Grounded Pointers
Grounded PointersGrounded Pointers
Grounded Pointers
 
C# and java comparing programming languages
C# and java  comparing programming languagesC# and java  comparing programming languages
C# and java comparing programming languages
 
Google mock training
Google mock trainingGoogle mock training
Google mock training
 
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
A Bonus to the "Three Interviews About Static Analyzers" Article, or Intervie...
 
Dot Net Accenture
Dot Net AccentureDot Net Accenture
Dot Net Accenture
 
Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016
 
Cinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patternsCinci ug-january2011-anti-patterns
Cinci ug-january2011-anti-patterns
 
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
6 Principles for Enabling Build/Measure/Learn: Lean Engineering in Action
 

Mehr von Paul Houle

Chatbots in 2017 -- Ithaca Talk Dec 6
Chatbots in 2017 -- Ithaca Talk Dec 6Chatbots in 2017 -- Ithaca Talk Dec 6
Chatbots in 2017 -- Ithaca Talk Dec 6Paul Houle
 
Estimating the Software Product Value during the Development Process
Estimating the Software Product Value during the Development ProcessEstimating the Software Product Value during the Development Process
Estimating the Software Product Value during the Development ProcessPaul Houle
 
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...Paul Houle
 
Fixing a leaky bucket; Observations on the Global LEI System
Fixing a leaky bucket; Observations on the Global LEI SystemFixing a leaky bucket; Observations on the Global LEI System
Fixing a leaky bucket; Observations on the Global LEI SystemPaul Houle
 
Cisco Fog Strategy For Big and Smart Data
Cisco Fog Strategy For Big and Smart DataCisco Fog Strategy For Big and Smart Data
Cisco Fog Strategy For Big and Smart DataPaul Houle
 
Making the semantic web work
Making the semantic web workMaking the semantic web work
Making the semantic web workPaul Houle
 
Ontology2 platform
Ontology2 platformOntology2 platform
Ontology2 platformPaul Houle
 
Ontology2 Platform Evolution
Ontology2 Platform EvolutionOntology2 Platform Evolution
Ontology2 Platform EvolutionPaul Houle
 
Paul houle the supermen
Paul houle   the supermenPaul houle   the supermen
Paul houle the supermenPaul Houle
 
Paul houle what ails enterprise search
Paul houle   what ails enterprise search Paul houle   what ails enterprise search
Paul houle what ails enterprise search Paul Houle
 
Subjective Importance Smackdown
Subjective Importance SmackdownSubjective Importance Smackdown
Subjective Importance SmackdownPaul Houle
 
Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Paul Houle
 
Dropping unique constraints in sql server
Dropping unique constraints in sql serverDropping unique constraints in sql server
Dropping unique constraints in sql serverPaul Houle
 
Paul houle resume
Paul houle resumePaul houle resume
Paul houle resumePaul Houle
 
Keeping track of state in asynchronous callbacks
Keeping track of state in asynchronous callbacksKeeping track of state in asynchronous callbacks
Keeping track of state in asynchronous callbacksPaul Houle
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHPPaul Houle
 
Once asynchronous, always asynchronous
Once asynchronous, always asynchronousOnce asynchronous, always asynchronous
Once asynchronous, always asynchronousPaul Houle
 
What do you do when you’ve caught an exception?
What do you do when you’ve caught an exception?What do you do when you’ve caught an exception?
What do you do when you’ve caught an exception?Paul Houle
 
Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Paul Houle
 
Pro align snap 2
Pro align snap 2Pro align snap 2
Pro align snap 2Paul Houle
 

Mehr von Paul Houle (20)

Chatbots in 2017 -- Ithaca Talk Dec 6
Chatbots in 2017 -- Ithaca Talk Dec 6Chatbots in 2017 -- Ithaca Talk Dec 6
Chatbots in 2017 -- Ithaca Talk Dec 6
 
Estimating the Software Product Value during the Development Process
Estimating the Software Product Value during the Development ProcessEstimating the Software Product Value during the Development Process
Estimating the Software Product Value during the Development Process
 
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
Universal Standards for LEI and other Corporate Reference Data: Enabling risk...
 
Fixing a leaky bucket; Observations on the Global LEI System
Fixing a leaky bucket; Observations on the Global LEI SystemFixing a leaky bucket; Observations on the Global LEI System
Fixing a leaky bucket; Observations on the Global LEI System
 
Cisco Fog Strategy For Big and Smart Data
Cisco Fog Strategy For Big and Smart DataCisco Fog Strategy For Big and Smart Data
Cisco Fog Strategy For Big and Smart Data
 
Making the semantic web work
Making the semantic web workMaking the semantic web work
Making the semantic web work
 
Ontology2 platform
Ontology2 platformOntology2 platform
Ontology2 platform
 
Ontology2 Platform Evolution
Ontology2 Platform EvolutionOntology2 Platform Evolution
Ontology2 Platform Evolution
 
Paul houle the supermen
Paul houle   the supermenPaul houle   the supermen
Paul houle the supermen
 
Paul houle what ails enterprise search
Paul houle   what ails enterprise search Paul houle   what ails enterprise search
Paul houle what ails enterprise search
 
Subjective Importance Smackdown
Subjective Importance SmackdownSubjective Importance Smackdown
Subjective Importance Smackdown
 
Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#
 
Dropping unique constraints in sql server
Dropping unique constraints in sql serverDropping unique constraints in sql server
Dropping unique constraints in sql server
 
Paul houle resume
Paul houle resumePaul houle resume
Paul houle resume
 
Keeping track of state in asynchronous callbacks
Keeping track of state in asynchronous callbacksKeeping track of state in asynchronous callbacks
Keeping track of state in asynchronous callbacks
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHP
 
Once asynchronous, always asynchronous
Once asynchronous, always asynchronousOnce asynchronous, always asynchronous
Once asynchronous, always asynchronous
 
What do you do when you’ve caught an exception?
What do you do when you’ve caught an exception?What do you do when you’ve caught an exception?
What do you do when you’ve caught an exception?
 
Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#Extension methods, nulls, namespaces and precedence in c#
Extension methods, nulls, namespaces and precedence in c#
 
Pro align snap 2
Pro align snap 2Pro align snap 2
Pro align snap 2
 

Kürzlich hochgeladen

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Kürzlich hochgeladen (20)

The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

Prefix casting versus as-casting in c#

  • 1. Generation 5 » Prefix-casting versus as-casting in C#  Subscribe to our RSS Feed | About Us Prefix-casting versus as-casting in C# Introduction This is a story of two types: GenericType and SpecificType, where GenericType is a superclass of SpecificType. There are two types of explicit cast in C#: The Prefix cast: [01] GenericType g=...; [02] SpecificType t=(SpecificType) g; The as cast: [03] GenericType g=...; [04] SpecificType t=g as SpecificType; Most programmers have a habit of using one or the other — this isn’t usually a conscious decision, but more of a function of which form a programmer saw first. I, for instance, programmed in Java before I learned C#, so I was already in the prefix cast habit. People with a Visual Basic background often do the opposite. There are real differences between the two casting operators which can make a difference in the reliability and performance of your application. Prefix casting: Reliable Casting The major difference between prefix- and as-casting is what happens when the cast fails. Imagine, for instance, that g is really an instance of AnotherSpecificType, which is not a subclass of SpecificType. In this case, the prefix-cast throws an exception at line [2] — however, the as-cast returns null when the cast fails, letting the execution of the program barrel on. It’s easier to implement correct error handling in programs that use prefix-casting, and programs that use prefix-casting are easier to debug. Prefix-casting causes an exception to be thrown at the moment of the cast, where the problem is obvious. Ascasting, on the other hand, can cause an exception to be thrown at the moment where the SpecificType t is referenced. The used of the SpecificType can be far away in the program: it can be in another method, or another class — it could even happen hours after the cast is performed. Be it in development or production, bugs caused by corrupted data structures can be maddeningly difficult to find and correct. As-casting: Fast Casting If it’s harder to write correct programs with as-casting, why would anybody use it? For one thing, as-casting is faster than prefix casting by quite a lot. Benchmarks show that as-casting is about five times faster than prefix casting. That said, the performance of casting is only going to matter in the innermost of loops of most programs. The fastest kind of casting is no casting at all: it’s best to use generics to eliminate the need for casting when possible and to move casting outside loops. (Generics also improve the reliability of your code because they help C#’s static type checking catch mistakes.) There are some cases where as-casting could be convenient, for instance, when you expect the cast to fail sometimes. Often I like to ‘tag’ classes with interfaces that specify certain behaviors. For example, [05] public Interface IBoppable { [06] void Bop(); [07] } Now i might want to loop through a bunch of Thing objects, and bop all the ones that implement IBoppable: it’s reasonable to use as-casting here: [08] List<Thing> list=... [09] foreach(Thing thing in list) { http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/[1/16/2014 3:55:16 PM] Search for: Search Archives June 2012 (1) August 2010 (1) May 2010 (1) June 2009 (2) April 2009 (1) March 2009 (1) February 2009 (3) January 2009 (3) November 2008 (1) August 2008 (2) July 2008 (5) June 2008 (5) May 2008 (2) April 2008 (6) March 2008 (8) June 2006 (1) February 2006 (1) Categories AJAX (2) Asynchronous Communications (16) Biology (1) Books (1) Design (1) Distributed (1) Exceptions (2) Functional Programming (1) GIS (1) Ithaca (1) Japan (1) Math (1) Media (3) Nature (1) Semantic Web (3) Tools (28) CRUD (1) Dot Net (17) Freebase (2) GWT (9) Java (7) Linq (2) PHP (6) Server Frameworks (1) Silverlight (12) SQL (5) Uncategorized (1) Web (2) Analytics (1)
  • 2. Generation 5 » Prefix-casting versus as-casting in C# [10] [11] [12] [13] [14] } List boppable=thing as IBoppable; if (boppable !=null) { boppable.Bop() } It’s OK to use as-casting if you’re going to check to see if the value is null immediately after the cast. The above code is correct, but has the bad smell that the boppable variable continues to exist in the block after the cast… It’s still there for a later maintainer to use erroneously. In cases like this, code can be made clearer with the is operator: [15] List<Thing> list=... [16] foreach(Thing thing in list) { [17] if(thing is IBoppable) { [18] ((IBoppable) boppable).Bop() [19] } [20] } (Speed freaks can use as-cast on line 18, as we know it’s not going to fail.) The pattern of testing for null after an as-cast has a few problems. (i) It doesn’t distinguish between the case of the original object being null from the case of the original object being the wrong type and (ii) correct error handling often requires more contorted logic than using an exception — and once you added test logic, you’ve lost the speed advantage of as-casting. Conclusion C# offers two casting operators: the prefix-cast and the as-cast. Although the two operators compile to different op-codes in the CLR, the practical difference between them is in how they handle failed casts. Prefix-cast throws an exception on cast failure, while as-cast returns null . It’s easier to implement correct error handling when you use prefix cast, because it doesn’t require manual checks for null values that can cause problems in distant parts of your program. Prefix-cast should be the default cast operator on your fingertips, that you use for everyday situations — reserve as-cast for special cases where performance matters. (For best performance, however, eliminate the cast entirely.) Paul Houle on June 13th 2008 in Dot Net Comments (13) Comments (13) Login Sort by: Date Rating Last Activity Michael james · 291 weeks ago +2 Great post, i learnt something new today. I always use type cast but have colleagues that use as. Knowing the difference will be a great help Reply Will · 291 weeks ago 0 I disagree. I'd say the rule of thumb is: If you cannot continue if the cast fails, then use the prefix style cast, which will result in an exception. If you can continue if the cast fails, then use a combination of "if object is type ... " and "as" casts. You should NEVER do this: MyType t; try { t = (MyType)foo; } catch { // do something here if cast fails and return } // do something here if cast succeeds and return That's a situation where "is" and "as" operators are useful. An example would be checking addins to see if they implement IDisposable. You won't know this at compile time, so you'll have to make some type of check. I've found that the majority of time I can handle invalid casts more often than not, so the majority of http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/[1/16/2014 3:55:16 PM]
  • 3. Generation 5 » Prefix-casting versus as-casting in C# time I'm using "is" and "as". Reply Janko · 291 weeks ago 0 Very good article! Reply cristian · 291 weeks ago 0 Hi there, nice post. I think you should mention or write a post about about explicit type conversion. It's an important distinction of "prefix-casting". Reply Sebastian · 291 weeks ago 0 I don't think articles about performance in .Net 1.1 should be used as a reference in 2008. Just read the article's comments or try it yourself before making such conclusions. IF you want performance then do the _exact opposite_ of your "IBobbable" example. The first snippet (as) will use isinst (and a branch instruction) whereas the is-cast snippet will use isinst and castclass. A quick test shows a performance gain of about 12%, whew! This as-null pattern is the only place I use that operator. And now guess which use of "as" can be found in the "as keyword [C#]" SDK article ;) . Reply Tim C · 291 weeks ago 0 "as" is faster? Sample code to back that up? In my testing it was for all intents and purposes EXACTLY the same. Reply Zack Owens · 291 weeks ago +1 I'm not quite sure there is a situation that would require one type of cast over the other when you are dealing with classes or interfaces. Both are interchangeable syntactically and logically. But also keep in mind that the as operator will only work for classes and interfaces. You must use the prefix cast when working with an enum or struct. The is operator, however, will work with enums and structs. Reply Maarten Oosterhoff · 291 weeks ago 0 Another difference is that value types cannot be cast using the 'as' keyword, you must us prefix-casting for value-types. I usually use the is/as keyword, I find that works best for me to understand the code. Nice article though! Reply Petar Petrov · 291 weeks ago 0 I totally agree with Will. I always use "as" operator over "is"+"as". However there is situations where I want to force the type so I use the prefix cast. Reply Anu Viswan · 281 weeks ago Hi I would disagree with you on the latter part in which you mentioned "as" casting is faster. Can you provide some code sample to support your claim ?? http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/[1/16/2014 3:55:16 PM] 0
  • 4. Generation 5 » Prefix-casting versus as-casting in C# Thanks Anu Reply Paul Houle · 281 weeks ago +2 @Anu, according to reports I've seen, the performance of as- and prefix casts have largely converged as of .net 3.5. It would be interesting to run tests on mono as well. At this point, I think people should use whatever convention they think is better from a sofware engineering standpoint. In most cases, I think it's better to get an exception early rather than face a null pointer deference later. On the other hand, there are definitely cases where the as-cast semantics are exactly what you want. Reply Anu Viswan · 281 weeks ago 0 Thanks Paul for your kind reply. I understand the speed difference is trivial and doesnt really makes too much impact in real life application. Only thing that would matter is how each handle exception, which again you were spot on that its better to catch an exception earlier than face null later. I was just curious about the differnce. After i read your article, i tried out my little study on differnce and it seems like prefix casting uses "castclass" function in the IL code generated while "as" casting would use isinst. Reading about these IL commands, made me wunder again. Did you happen to check out the differnce in IL ? Reply BlahaK · 79 weeks ago 0 Hi, could anybody help me? I am trying to use prefix-casting dynamicaly by this way: ' long userID = (Type.GetType("long"))mySqlDataReader.GetValue(i); ' Of course it does not work, maybe because "long" is "struct"? .NET says "Cannot implicitly convert 'System.Type' to 'long'. So my question is: how to do this? Thanks for your help. Reply Post a new comment Enter text right here! Comment as a Guest, or login: Name Email Website (optional) Displayed next to your comments. Not displayed publicly. If you have a website, link to it here. None Subscribe to None Submit Comment Copyright © 2013 Generation 5. WordPress Theme design. http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/[1/16/2014 3:55:16 PM]