TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
C# Coding Standard
Search

Rule:  12@101

Synopsis:Avoid boxing and unboxing of value types
Language:C#
Severity Level:6
Category:Performance


Description:

Boxing and unboxing of values types is an expensive operation and should be avoided.

Some ways to achieve this:

  • If there is only one type of object in the ArrayList, then replace it by the generic List<> version. Besides the performance gain, the added bonus is type safety. Note: some other collection classes also have generic versions. And when you write your own classes, consider making it a generic class also.
  • When putting a value type into a string formatting, use method ToString() on the value type. E.g. instead of writing String.Format("SomeValue = {0}", x); write String.Format("SomeValue = {0}", x.ToString()).