One of the new features in .NET 6 and C¤ 10 is support for global usings. We can define using declarations as global and they work all over the project so we don’t have to define them in other files anymore. Here’s how it works.
Sample project
Suppose we have have console program that runs on Task Scheduler and performs some nightly tasks. Screenshot of Solution Explorer on right illustrates what we have. There are some classes to access local file system and cloud storage. There are also some classes to perform import tasks.
Classes that move data from one place to another usually have some repeated using directives. One good example is System.Data and its child namespaces. Everytime we add new class we have to add those using directives again. This holds also true for importer classes shown on screenshot.
Here’s the fragment on one importer class.
using System.Data; using System.Data.SqlClient; using ConsoleApp4.FileAccess; namespace ConsoleApp4.ImportExport; public class CustomersImporter {
private readonly IFileClient _fileClient;
private readonly string _connectionString;
public CustomersImporter(IFileClient fileClient, string connectionString)
{
_fileClient = fileClient;
_connectionString = connectionString;
}
// ... }
Three using declarations – first three lines of code above – are present also in other import and export classes.
Introducing global usings
C# 10 introduces global usings that are effective over whole project. Let’s add new file GlobalUsings.cs to our project. In this file we define usings that must be available in every class and interface file in our project.
global using System.Data; global using System.Data.SqlClient; global using ConsoleApp4.FileAccess;
We can remove these usings from all other files in our project.
Don’t put global usings to same file with class or interface. It’s easy to create a mess when global usings are defined in same file with classes or interfaces. Always keep global usings in separate file.
ASP.NET Core 6 application
Global usings work also in ASP.NET Core applications. There’s _ViewImports.cs file in ASP.NET Core where we can define usings for all views in project. Here’s the example of _ViewImports.cs file after creating a new ASP.NET Core MVC project.
@using WebApplication1 @using WebApplication1.Models @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Global usings work also for ASP.NET Core MVC views and Razor pages. Instead of keeping usings in _ViewImports.cs file we can make them global in global usings file.
global using System.Diagnostics; global using Microsoft.AspNetCore.Mvc; global using Microsoft.AspNetCore.Mvc.RazorPages;
Not so fast… Before abandoning _ViewImports.cs for using declarations think for a moment. Views can be taken as a separate context from all other code in your web application. Using declarations for views may be not needed in code files of your application. Don’t pollute your classes with using declarations you only use in views.
Wrapping up
Global usings is useful feature introduced in C# 10. It may also come specially handy when working with code that uses (too) many namespaces. Global usings are also good feature to keep code files cleaner and define all most needed usings in same file. Luckily it works also with ASP.NET Core MVC views and Razor pages.
View Comments (1478)
Hello, your customer service is very fast and solution-oriented. Thank you for resolving the issue I experienced quickly.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, the discounts and campaigns on your website are very advantageous for dermocosmetics and supplements. This allows me to shop at very affordable prices.
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, the blog posts on your website are very helpful and informative in the dermocosmetics and supplements fields. I will visit frequently to learn new things.
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, the shipping time for your dermocosmetics products is very fast. I received my order quickly.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, your customer service is very fast and solution-oriented. Thank you for resolving the issue I experienced quickly.
Hello, the blog posts on your website are very helpful and informative in the dermocosmetics and supplements fields. I will visit frequently to learn new things.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, the discounts and campaigns on your website are very advantageous for dermocosmetics and supplements. This allows me to shop at very affordable prices.
Hello, the shipping time for your dermocosmetics products is very fast. I received my order quickly.
Hello, the blog posts on your website are very helpful and informative in the dermocosmetics and supplements fields. I will visit frequently to learn new things.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, I will recommend your website to all my friends and family.
Hello, the design of your website is very beautiful and eye-catching. It is also very useful that it is mobile friendly.
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, the design of your website is very beautiful and eye-catching. It is also very useful that it is mobile friendly.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, the discounts and campaigns on your website are very advantageous for dermocosmetics and supplements. This allows me to shop at very affordable prices.
Hello, the shipping time for your dermocosmetics products is very fast. I received my order quickly.
Hello, the design of your website is very beautiful and eye-catching. It is also very useful that it is mobile friendly.
Hello, I will recommend your website to all my friends and family.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, the discounts and campaigns on your website are very advantageous for dermocosmetics and supplements. This allows me to shop at very affordable prices.
Hello, I will recommend your website to all my friends and family.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, the shipping time for your dermocosmetics products is very fast. I received my order quickly.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, your customer service is very fast and solution-oriented. Thank you for resolving the issue I experienced quickly.
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, I will recommend your website to all my friends and family.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, the discounts and campaigns on your website are very advantageous for dermocosmetics and supplements. This allows me to shop at very affordable prices.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, the design of your website is very beautiful and eye-catching. It is also very useful that it is mobile friendly.
Hello, the design of your website is very beautiful and eye-catching. It is also very useful that it is mobile friendly.
Hello, I will recommend your website to all my friends and family.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, the blog posts on your website are very helpful and informative in the dermocosmetics and supplements fields. I will visit frequently to learn new things.
Hello, I will recommend your website to all my friends and family.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, your customer service is very fast and solution-oriented. Thank you for resolving the issue I experienced quickly.
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, I appreciate that you provide detailed information about your dermocosmetics products. This allows me to make informed choices about the products I purchase.
Hello, the shipping time for your dermocosmetics products is very fast. I received my order quickly.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, the shipping time for your dermocosmetics products is very fast. I received my order quickly.
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, the blog posts on your website are very helpful and informative in the dermocosmetics and supplements fields. I will visit frequently to learn new things.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, I will recommend your website to all my friends and family.
Hello, the product images on your website are very high quality and reflect the real appearance of the products. This allows me to better understand the products.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, the payment methods on your website are very diverse and secure. This allows me to shop with confidence.
Hello, I really liked the product descriptions on your website. They are very detailed and informative. This allows me to have complete information about the products.
Hello, I will recommend your website to all my friends and family.
Hello, the blog posts on your website are very helpful and informative in the dermocosmetics and supplements fields. I will visit frequently to learn new things.
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, your website is very useful and informative. I was able to easily find everything I was looking for. Thank you!
Hello, I browsed your website and I really liked it. I was particularly interested in the dermocosmetics and supplements categories. I will visit again later to review these products in more detail.