Bia Securities

.Net performance: Boxing vs. parsing

by on Mar.09, 2005, under Uncategorized

I just asked this in a performance chat w/ Rico. It might be of use to some developers that are curious about these sorts of things…

Q: Hey Rico. How much of a performance difference is there between boxing vs. parsing. For instance:
return (bool)ViewState["myvar"];
vs.
return bool.parse(ViewState["myvar"].ToString());

A: Parsing is a total disaster compared to boxing. ToString will make a string then it will be parsed. Actually in that example there’s just unboxing which is really cheap. Very few cases of boxing would ever be more expensive than making a temp string — it’s not *that* bad to box

This was mainly due to an FxCop rule that says that it is bad to box values. Apparently this is one case where boxing is much better than the alternative.


Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!