C# Implicit Usings — Complete Guide
In this tutorial, you'll learn about C# Implicit Usings. We cover key concepts, practical examples, and best practices.
You create a new .NET 6+ console app, open Program.cs, and notice there are no using System; lines — yet Console.WriteLine works. Implicit usings automatically import common namespaces based on your project SDK.
Without implicit usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Console.WriteLine("Hello");
Output: "Hello" — but you had to add four using lines.
With implicit usings (default in .NET 6+)
Console.WriteLine("Hello");
Output: "Hello". The SDK generates a hidden GlobalUsings.cs with the appropriate using directives for your project type.
The auto-generated usings depend on the SDK:
| SDK | Auto-imported namespaces |
|---|---|
Microsoft.NET.Sdk |
System, System.Collections.Generic, System.IO, System.Linq, System.Threading, etc. |
Microsoft.NET.Sdk.Web |
Above + System.Net.Http, Microsoft.AspNetCore.* |
Microsoft.NET.Sdk.Worker |
Above + Microsoft.Extensions.* |
Prevention
- Keep implicit usings enabled (default) for new projects.
- Disable with
<ImplicitUsings>disable</ImplicitUsings>in.csprojif you prefer explicit control. - Add
global usingfor namespaces not covered by implicit usings. - Use
<Using>items in.csprojto add custom implicit usings without code. - Be aware that different SDKs import different namespaces — test when switching SDK types.
- Use
dotnet new console— the template uses implicit usings by default.
Common Mistakes with implicit using
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Implicit usings are enabled in all DodaTech projects. For more .NET tips, visit DodaTech.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro