As a practical example of time difference in populating a List with boxed objects vs. typevalue int, the following console application code excerpt gives (in my computer) the following results:
Time without boxing: 156,250
Time with boxing: 1,093,750
That is, the objects list needs 7 times the ticks than simple int typevalue list to build.
//Code:
List<int> intsList = new List<int>();
List<object> objectsList = new List<object>();
DateTime startTimeToFillIntList = DateTime.Now;
for (int i = 0; i < 1000000; i++) { intsList.Add(i); }
DateTime endTimeToFillIntList = DateTime.Now;
Console.WriteLine("Time without boxing: " + (endTimeToFillIntList.Ticks - startTimeToFillIntList.Ticks).ToString());
DateTime startTimeToFillObjList = DateTime.Now;
for (int i = 0; i < 1000000; i++) { objectsList.Add(i); }
DateTime endTimeToFillObjList = DateTime.Now;
Console.WriteLine("Time with boxing: " + (endTimeToFillObjList.Ticks - startTimeToFillObjList.Ticks).ToString());
//End code
This demonstrates the Microsoft hints about avoiding unneeded boxing/unboxing in repetitive assignments, as stated in the Microsoft 70-536 certification book "Microsoft .NET Framework Application Development Foundation" second edition, Tony Northrup, Chapter 1 Framework fundamentals, Lesson 4 Converting between types, paragraph "What are boxing and unboxing?"
Happy coding with this "Try avoid unneeded boxing" pattern always in mind!
Giuseppe Giordano