Search code examples
c#performanceoptimizationnamespacesxna

Is performance affected by using namespaces in C#?


I'm creating a game in XNA 4.0 and C#. Whenever I create a new class, it always generates the following four lines of code at the top.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

In most cases, these namespaces are not necessary to use in my classes as I do not need to explicitly call the methods that they provide. Should I remove them from each class where they are not being used? Is there an impact on performance if I leave these lines of code in?


Solution

  • Namespaces are "consumed" by the compiler, and aren't present in the compiled code. So the compiled code is unaffected by the number of the using statesment present. Clearly the compiler will be slown a little if there are too many usings (but only a little).

    You can see it from here: http://goo.gl/T5TIYx On the right pane there is the IL code that is "generated" by the compiler. There is no trace of the various usings.

    The compiler will be slowed a little if you have too many using because when you reference a type, it has to check for each namespace if there is the type you are referencing, and it can't stop at the first "success", because the C# compiler tells you even if there is some ambiguity (class with same name in two different namespaces)