Using inside or outside namespace? Impact on C# Performance

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Clash of the Titans: using inside or outside namespace in C#

\n\n

If you've ever written in C#, you've surely seen this debate in comments or on Stack Overflow. Some developers stubbornly place using inside namespace, others — outside. And each is convinced their approach is the only correct one. But who is right? And most importantly — does it affect the performance of your code?

\n\n

Let's settle this question once and for all, look under the hood of the compiler, and find out what actually happens to your using directives. Spoiler: the answer will surprise you.

\n\n

What exactly are using directives and why are they needed?

\n\n

Before diving into the details, let's recall the basics. using directives are your pass to the world of other namespaces. Without them, you would have to write fully qualified names every time:

\n\n
System.Console.WriteLine("Hello, world!");\nSystem.Collections.Generic.List<int> numbers = new System.Collections.Generic.List<int>();
\n\n

Agreed, it looks terrible. using directives make the code cleaner and more readable:

\n\n
using System;\nusing System.Collections.Generic;\n\nConsole.WriteLine("Hello, world!");\nList<int> numbers = new List<int>();
\n\n

But where to place them — that's the question that has plagued generations of C# developers.

\n\n

Two camps: outside vs inside

\n\n

Here's what the two approaches look like:

\n\n

Option 1: using outside namespace

\n\n
using System;\nusing System.Collections.Generic;\n\nnamespace MyApp\n{\n    class Program\n    {\n        static void Main()\n        {\n            List<int> list = new List<int>();\n        }\n    }\n}
\n\n

Option 2: using inside namespace

\n\n
namespace MyApp\n{\n    using System;\n    using System.Collections.Generic;\n\n    class Program\n    {\n        static void Main()\n        {\n            List<int> list = new List<int>();\n        }\n    }\n}
\n\n

At first glance, the only difference is indentation. But the community has split into two irreconcilable camps. Let's examine the arguments of each.

\n\n

Argument #1: Performance and compilation

\n\n

The most common question: does the placement of using affect performance? The short answer: NO. And here's why.

\n\n

using directives are purely syntactic sugar for the compiler. They exist only during the compilation phase. When your code is transformed into IL (Intermediate Language), no trace of the using directives remains. The compiler simply uses them to resolve type names and then discards them.

\n\n

Let's prove this in practice. Consider this code:

\n\n
// File: Program.cs\nusing System;\nusing System.Collections.Generic;\n\nnamespace MyApp\n{\n    class Program\n    {\n        static void Main()\n        {\n            List<int> list = new List<int>();\n            Console.WriteLine(list.Count);\n        }\n    }\n}
\n\n

After compilation into IL code, you will see something like this (greatly simplified):

\n\n
.method private hidebysig static void Main() cil managed\n{\n    .entrypoint\n    .maxstack 1\n    .locals init (\n        [0] class [System.Runtime]System.Collections.Generic.List`1<int32> list\n    )\n    \n    // Creating List<int>\n    newobj instance void class [System.Runtime]System.Collections.Generic.List`1<int32>::.ctor()\n    stloc.0\n    \n    // Calling Console.WriteLine\n    ldloc.0\n    callvirt instance int32 class [System.Runtime]System.Collections.Generic.List`1<int32>::get_Count()\n    call void [System.Console]System.Console::WriteLine(int32)\n    \n    ret\n}
\n\n

Notice: in the IL code, fully qualified names are used (System.Collections.Generic.List`1,

Blogs

Book Recommendations