Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are the using statements/directives in .NET 6

I got up and running with Visual Studio 2022 Preview for a couple of days now.

Got the first shock, there is no Startup.cs. Thats ok, a bit of reading, I know Startup is removed.

Today got another slap. I see no using statements. Here it is.

I just created a brand new .NET 6 web app and as I hover over the WebApplication class, I realized it stays in Microsoft.AspNetCore.Builder namespace. And the generated Program.cs class looks like this.

Program.cs class in ASP.NET core 6 Web app

So where is the using Microsoft.AspNetCore.Builder; statement?

Whats the magic? Why is .net becoming mystical by the day?

The full Program.cs file is as follows.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();
like image 638
VivekDev Avatar asked Oct 27 '21 12:10

VivekDev


People also ask

What are using directives in C#?

The using directive allows you to use types defined in a namespace without specifying the fully qualified namespace of that type. In its basic form, the using directive imports all the types from a single namespace, as shown in the following example: C# Copy. using System.

What is using directive in namespace?

A using directive provides access to all namespace qualifiers and the scope operator. This is accomplished by applying the using keyword to a namespace identifier.

How do I add a directive in Visual Studio?

Run regedit . If you want to install the directive processor in the experimental version of Visual Studio, insert "Exp" after "11.0". Add a registry key that has the same name as the directive processor class. In the registry tree, right-click the DirectiveProcessors node, point to New, and then click Key.

Are you missing a using directive or an assembly reference in MVC?

Open your Project within the Solution Explorer. Right-click the References folder and Choose Add Reference.... Find and select the assembly that cooresponds to your error message or class. Click the Ok button to add it to your Project.

Where should a directive be placed in a source code file?

At the beginning of a source code file, before any namespace or type declarations. In any namespace, but before any namespaces or types declared in that namespace, unless the global modifier is used, in which case the directive must appear before all namespace and type declarations. Otherwise, compiler error CS1529 is generated.

What is a using directive in Visual Studio?

In its basic form, the using directive imports all the types from a single namespace, as shown in the following example: You can apply two modifiers to a using directive: The global modifier has the same effect as adding the same using directive to every source file in your project.

What is the difference between using directive and using statements?

Fun! using directive. using statements are those which appear as statements in bodies and control Disposal. And if you use the right phrase and go the the documentation for using directive you'd have seen this new behaviour explained. @Damien_The_Unbeliever, yes its using directive.

Where do you put global using directives in a file?

All global using directives in a single file must appear before: All using directives without the global modifier. All namespace and type declarations in the file. You may add global using directives to any source file. Typically, you'll want to keep them in a single location.


1 Answers

C# 10.0 introduces a new feature called global using directive (global using <fully-qualified-namespace>;) which allows to specify namespaces to be implicitly imported in all files in the compilation. .NET 6 RC1 has this feature enabled by default in new project templates (see <ImplicitUsings>enable</ImplicitUsings> property in your .csproj).

For Microsoft.NET.Sdk.Web next namespaces should be implicitly imported (plus the ones from Microsoft.NET.Sdk):

  • 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

UPD

To address your questions in comment:

At the moment of writing the generated file containing default imports will be inside the obj folder named something like ProjectName.GlobalUsings.g.cs.

To modify default imports you can add Using element to your .csproj file. Based on exposed attributes it allows several actions including addition and removal:

<ItemGroup>
    <Using Include="SomeFullyQualifiedNamespace"/>
</ItemGroup>

For just addition you can simply prefix your using directive with global modifier in any file (or create a separate one just for this):

global using SomeFullyQualifiedNamespace;
like image 52
Guru Stron Avatar answered Oct 31 '22 16:10

Guru Stron