.NET 6 has just been released publicly, and things couldn't get any better! This release came with a lot of goodies, and aids to increases speed and efficiency for writing code. Here, I'll be demonstrating some of my favourite features that were shipped with this release.
Global using
Directive
You know how stressful (and a waste of space) it is to see something like this in almost every file of your ASP.NET Core project?
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Things just got better. For using
directives that are common in a lot of files, you could now use the global using
to declare them in a file โ preferably, create a globals.cs or usings.cs or imports.cs file for this. These would automatically be imported for every file in the project. Pretty cool, isn't it? ๐
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.AspNetCore.HttpsPolicy;
global using Microsoft.Extensions.Configuration;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;
File-Scoped Namespace Declaration
This reduces the indentation and amount of curly braces significantly (especially when accompanied with top-level statements). Formerly, namespaces were declared this way:
namespace MyNamespace
{
public class MyClass
{ ... }
}
With file-scoped namespaces, it drops to this:
namespace MyNamespace;
public class MyClass
{ ... }
Note however, that the namespace must be declared at the top โ that is, after the using
directives and before any declaration is made.
Null Parameter Checking
Previously, if we wanted to check if arguments passed to a method or function were null, we'd do something like this:
public Task<bool> UpdateLocationAsync(int id, Point location)
{
if (location == null)
{
throw new ArgumentNullException("location");
}
// Logic goes here...
}
In C# 10, throwing the ArgumentNullException
can be done automatically for a parameter by adding "!!" (sounds like a warning ๐
๐ฝโโ๏ธโ ) behind the parameter.
public Task<bool> UpdateLocationAsync(int id, Point location!!)
{
// Logic goes here...
}
Minimal Web API
The final one for today. This one is great for beginners who have no experience with MVC (Model-View-Controller), or in cases where you need to create a quick API. It's also good one if you want minimal dependencies and not a lot of features in the web app. When you create the app, only a single file is created which would have a template like the one below.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateTime.Now.AddDays(index),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.Run();
internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
The function app.MapGet()
works as the GET endpoint of the API with the route set as ~/weatherforecast
.
You can add a simple POST endpoint like this:
app.MapPost("/test", () => Results.Ok("Request received!"));
It's that easy!
PS: Okay, I don't know why I called this article .NET 6 Ninjas, but I love it ๐. Anyway, this is just a bit of what .NET 6 offers . To find out more, check out devblogs.microsoft.com/dotnet/announcing-ne...