Implicit usings, top level statements, file scoped namespaces in dotnet

implicit usings

Implicit usings = no repeated using statement in all files

  • With implicit usings feature in a C# project, common using statements are automatically added by the compiler in all the C# files.
  • For example using System; statement is not required to run the dotnet console application as shown below
Console.WriteLine("Hello World!");

  • Additional implicit usings can be declared in a C# project with global usings statements in any one of the project’s C# files as shown below.
global using Ardalis.GuardClauses;
global using MediatR;
global using Project2.Utils;

  • Existing implicit usings can be removed or included in the .csproj file of the C# project as shown below
<ItemGroup>
  <Using Remove="System.Threading.Tasks" />
  <Using Include="System.Math" />
</ItemGroup>

  • The implicit usings added in a C# project is based on the project type as shown below
SDK Default namespaces
Microsoft.NET.Sdk System
System.Collections.Generic
System.IO
System.Linq
System.Net.Http
System.Threading
System.Threading.Tasks
Web Apps
(Microsoft.NET.Sdk.Web)
Microsoft.NET.Sdk namespaces
System.Net.Http.Json
Microsoft.AspNetCore.Builder
Microsoft.AspNetCore.Hosting
Microsoft.AspNetCore.Http
Microsoft.AspNetCore.Routing
Microsoft.Extensions.Configuration
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Hosting
Microsoft.Extensions.Logging
Worker Apps
(Microsoft.NET.Sdk.Worker)
Microsoft.NET.Sdk namespaces
Microsoft.Extensions.Configuration
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Hosting
Microsoft.Extensions.Logging
Microsoft.NET.Sdk.WindowsDesktop (Windows Forms) Microsoft.NET.Sdk namespaces
System.Drawing
System.Windows.Forms
Microsoft.NET.Sdk.WindowsDesktop (WPF) Microsoft.NET.Sdk namespaces
Removed System.IO
Removed System.Net.Http
  • The implicit usings are declared as global using statements in an auto-generated file inside the obj folder

Top level statements = no Main method

  • Without the top level statements, the entry point of a C# application would be like the following.
namespace Application
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

  • This is because every thing should be a class within a namespace in a C# project. To find the entry point of a C# project, the compiler searches for a class with static void Main(string[] args) method.
  • With top level statements feature enabled in a C# project, a C# file as shown below can be the entry point of a C# project
Console.WriteLine("Hello World!");

  • async await code can also be run in the entry file as shown below
Console.WriteLine("Hello World!");
await Task.Delay(50);
Console.WriteLine("Hello World!");

  • Command line arguments can be accessed using the args keyword as shown below
Console.WriteLine("Hello World!");
foreach (var s in args)
{
    Console.Write(s);
    Console.Write(' ');
}

  • Just like we cannot have multiple classes with static void main method, there can’t be multiple files using top level statements in a C# project. This will result in compilation error.

File scoped Namespaces = less indentation

  • A file in a C# project without File scoped namespacing can like the following
namespace MyCompany.MyProject
{
    public class MyClass
    {
        // Class implementation
    }

    public class AnotherClass
    {
        // Class implementation
    }
}

  • A file with file scoped namespaces can be like the following
namespace MyCompany.MyProject;  // Note the semicolon

public class MyClass
{
    // Class implementation
}

public class AnotherClass
{
    // Class implementation
}

  • Hence with file scoped namespaces, classes need not be written inside the namespace curly braces. This makes the code more readable and one indentation is reduced for classes
  • Only one file scoped namespace is allowed in a C# file. Nested namespaces or multiple namespaces are not allowed in a C# file
  • Namespace declaration should be immediately after the using statements at the top a C# file. (writing using statements after namespace declaration is also allowed but not recommended)

References

Comments

Popular posts from this blog

ClaimsPrincipal, ClaimsIdentity, Claims explained in dotnet authentication

Introduction to dotnet with a simple hello world example