DEV Community

Cover image for C# - Implicit Usings
Keyur Ramoliya
Keyur Ramoliya

Posted on

3

C# - Implicit Usings

C# 10.0 introduces the concept of 'implicit usings,' which automatically includes common namespace imports based on the project type. This feature reduces the need for repetitive using directives at the top of every file, leading to cleaner and more concise code.

Here’s how you can use implicit usings in your C# project:

  1. Enable Implicit Usings in Your Project File:
    Modify your .csproj file to include the <ImplicitUsings>enable</ImplicitUsings> property. This tells the compiler to automatically include a set of default namespaces common to the project type (e.g., console, web, class library).

  2. Simplify Source Files:
    With implicit usings enabled, you can remove common namespace imports from your source files, as they are automatically included.

Example:

Your .csproj file will look something like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>
Enter fullscreen mode Exit fullscreen mode

And in your C# files, you no longer need to include common usings:

// No need for 'using System;' or other common namespaces

Console.WriteLine("Hello, World!");
Enter fullscreen mode Exit fullscreen mode

By utilizing implicit usings, you can focus on the unique aspects of your code without the clutter of common namespace declarations. This feature is particularly beneficial for new developers or in educational settings where simplifying the code can help focus on learning concepts. However, for some complex projects, explicitly declaring usings may still be preferred for clarity and control.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay