This is a mirror of official site: http://jasper-net.blogspot.com/

The Performance of Arrays

| Monday, June 14, 2010
Stop me if you’ve heard this one, but here’s some information about how arrays perform, and a neat trick you can do to (possibly) get some performance back.

Some background
In .NET, arrays of reference types are covariant in their element type, but not safely. Eric, as always, has a post that goes into this more deeply if you want to refresh your memory. The upshot is that if you have a Derived[], you can convert it to a Base[] and use it that way. For instance,

class Base { }
class Derived : Base { }

class Program
{
   static void Main()
   {
       Derived[] derivedArray = new Derived[10];
       // This is the covariant conversion
       Base[] baseArray = derivedArray;

       for (int i = 0; i < baseArray.Length; ++i)
       {
           // Putting a Derived into our Base[] is ordinary polymorphism
           baseArray[i] = new Derived();
       }
   }
}

Read more: Chris Burrows' Blog

Posted via email from .NET Info

0 comments: