Wednesday 4 November 2015

ASP.NET Tips #45 - Remember that type casting and conversions incur performance penalties

If you must perform type casting and conversions, try doing as little of them as possible. For example, rather than ...

if (obj is SomeType) then { ((SomeType)obj) ……. };

… which causes two casts (one when performing the 'if' evaluation and one in the body of the If statement), use this:

if (obj is SomeType) then { ((SomeType)obj) ……. };

That way, the cast is only done once with the 'as' parameter. If it fails, the value is NULL. If not, you can go ahead and use it, resulting in only one cast.

For more information, see: https://msdn.microsoft.com/en-us/library/ms173105.aspx

No comments :

Post a Comment