Comments on: Prefix-casting versus as-casting in C# http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/ Towards Intelligent Systems Wed, 11 Jul 2012 12:59:08 +0000 hourly 1 http://wordpress.org/?v=3.0.3 By: BlahaK http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-42928 BlahaK Wed, 11 Jul 2012 12:59:08 +0000 http://gen5.info/q/?p=31#comment-42928 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. 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.

]]>
By: Anu Viswan http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-719 Anu Viswan Tue, 26 Aug 2008 17:38:12 +0000 http://gen5.info/q/?p=31#comment-719 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 ? 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 ?

]]>
By: Paul Houle http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-712 Paul Houle Mon, 25 Aug 2008 22:53:35 +0000 http://gen5.info/q/?p=31#comment-712 @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. @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.

]]>
By: Anu Viswan http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-710 Anu Viswan Mon, 25 Aug 2008 17:12:30 +0000 http://gen5.info/q/?p=31#comment-710 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 ?? Thanks Anu 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 ??

Thanks
Anu

]]>
By: Petar Petrov http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-529 Petar Petrov Thu, 19 Jun 2008 08:46:05 +0000 http://gen5.info/q/?p=31#comment-529 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. 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.

]]>
By: Maarten Oosterhoff http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-528 Maarten Oosterhoff Thu, 19 Jun 2008 07:15:34 +0000 http://gen5.info/q/?p=31#comment-528 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! 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!

]]>
By: Zack Owens http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-527 Zack Owens Wed, 18 Jun 2008 22:10:05 +0000 http://gen5.info/q/?p=31#comment-527 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. 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.

]]>
By: Tim C http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-526 Tim C Wed, 18 Jun 2008 20:20:22 +0000 http://gen5.info/q/?p=31#comment-526 "as" is faster? Sample code to back that up? In my testing it was for all intents and purposes EXACTLY the same. “as” is faster? Sample code to back that up? In my testing it was for all intents and purposes EXACTLY the same.

]]>
By: Sebastian http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-525 Sebastian Wed, 18 Jun 2008 19:53:42 +0000 http://gen5.info/q/?p=31#comment-525 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 ;) . 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 ;) .

]]>
By: cristian http://gen5.info/q/2008/06/13/prefix-casting-versus-as-casting-in-c/comment-page-1/#comment-524 cristian Wed, 18 Jun 2008 19:14:31 +0000 http://gen5.info/q/?p=31#comment-524 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". 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”.

]]>

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /hosting/sites/gen5.info/htdocs/q/wp-includes/class-snoopy.php on line 1148

Warning: fsockopen(): unable to connect to :80 (php_network_getaddresses: getaddrinfo failed: No address associated with hostname) in /hosting/sites/gen5.info/htdocs/q/wp-includes/class-snoopy.php on line 1148