.NET performance compilation tips

When it is about performance we should remember it is assumable that in our mind on critical parts (hot paths) which used most frequently. They will produce most noticeable (from performance perspective) results.

Related to VB.NET:

  • There are different casts available in VB.NET :
    • DirectCast() – most effective and strict operator among all mentioned below operators
    • CType() – less strict but more powerful, and it’s power comes on cost of performance. Not as fast as DirectCast() operator
    • TryCast() – analogue operator to as in C#
    • To__

DirectCast() considered quickest according a lot of articles. Some tests performed on different types supports this opinion. In case you use the direct cast, you have to check before that the types are the same, otherwise exception will be thrown. Tests performed with check mentioned above.


  • Recreate/Enlarge array
    • Redim Preserve
    • ArrayList


I think it pretty self explanatory 🙂

Related to .NET:

  • String() is most common pitfall because of boxing process occurs there since second parameter is an Object.
  • Lists should be created with reasonable size in advance. Since its underlying type is array, it creates new array each type when new element is added.

Test performed on List<int> with two scenarios: Fixed size and Non Fixed size for when adding 1M elements and 10M elements.


  • “Fixed size” describe scenario in which List<Int> created intentionally with size (i.e. List<int> list = new List<int>()).
  • “Non Fixed size” is one created without size (i.e. List<int> list = new List<int>())


  • When LINQ used, be aware of each time when invoked it creates new iterator. Advice is not to use LINQ on hot paths or transform it instead to foreach block (in most cases it possible)


If you want contribute to this compilation, please do. Sharing your knowledge can contribute to the common goal.