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()) .
|