myesn

myEsn2E9

hi
github

C#: global usings

Introduction#

Starting from .NET 6 and C# 10, the global using directive is supported, which is effective throughout the entire project.

Best Practices#

To avoid confusion, create a separate GlobalUsings.cs in the project, then declare the global using directive inside, for example:

global using System.Data;

This way, these using statements can be removed from other files in the project.

ASP.NET Core#

In ASP.NET Core projects, the current practice is to define references for all views in _ViewImport.cs, for example in an MVC project:

@using WebApplication1
@using WebApplication1.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Now we can use the [best practices](Best Practices) to globalize these usings. However, since views and application code are independent of each other, it is not recommended to globalize usings that are only used in views. It is best to continue using _ViewImport.cs to declare usings for all views.

References#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.