Skip to main content

How to Use a Namespace in C# with a using Directive

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Every type in .NET lives in a namespace: StringBuilder lives in System.Text, Dictionary in System.Collections.Generic. A using directive at the top of a file lets you write the short name, StringBuilder, instead of the full one every time.

Syntax​

Syntax
using <Namespace>;
PartWhat it does
usingImports the types of a namespace into this file.
<Namespace>The namespace, such as System.Text.
PositionAt the top of the file, before any other code.

Microsoft's using directive reference: "The using directive enables you to use types defined in a namespace without specifying the fully qualified namespace of that type."

The Example​

The example builds a greeting with StringBuilder, a class for assembling text piece by piece, after importing its namespace.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which starts with using System.Text;, creates a StringBuilder, appends Hello and , C#, and prints the result.

main.cs
using System.Text;

var builder = new StringBuilder();
builder.Append("Hello");
builder.Append(", C#");

Console.WriteLine(builder.ToString());

using System.Text; lets the code say StringBuilder. Without it, the code would have to say System.Text.StringBuilder.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
Hello, C#

A zsh terminal where dotnet run main.cs prints Hello, C#.

The command is the same in zsh, bash and PowerShell.

What Happens Without It​

Delete the first line and run it again. The compiler can't find the class:

Output
/Users/Shared/you/usingcheck/main.cs(2,19): error CS0246: The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?)

The build failed. Fix the build errors and run again.

(2,19) is the line and column: StringBuilder on line 2.

The message itself names the fix: a missing using directive.

Why Console Needs No using​

Console lives in the System namespace, yet the code never imports it. .NET projects, and single-file apps like this one, add implicit global using directives for a set of common namespaces. For the standard SDK, Microsoft's project SDK documentation lists System, System.Collections.Generic, System.IO, System.Linq, System.Net.Http, System.Threading and System.Threading.Tasks. System.Text isn't on the list, so it needs its own using.

Common Mistakes​

MistakeWhat happensDo this instead
Forgetting the using for a type outside the implicit namespaceserror CS0246: The type or namespace name '…' could not be foundAdd using for its namespace
Putting a using directive below codeerror CS1529: A using clause must precede all other elements defined in the namespace except extern alias declarationsKeep using directives at the top
Importing a namespace you don't useNothing breaks, but the file gets noisyRemove unused using lines

Troubleshooting​

SymptomLikely causeFix
error CS0246 for a type you know existsIts namespace isn't importedLook up the type's namespace in its documentation and add using
error CS0104: 'Timer' is an ambiguous reference between 'System.Timers.Timer' and 'System.Threading.Timer'Two imported namespaces, here System.Timers and the implicit System.Threading, both have that typeWrite the full name, such as System.Timers.Timer, for that use

FAQ​

What's the difference between using and global using?​

global using applies to every file in the project, not just the one it's in. The implicit usings above are global usings that .NET adds for you.

Does a using directive make the program bigger?​

No. It only lets you write shorter names; the program uses the same types either way.

Conclusion​

using Namespace; at the top of a file lets you use that namespace's types by their short names. .NET already imports common namespaces such as System, so add using for the rest, such as System.Text.

Additional Resources​