Rethink what you think you know about .Net

AWS recently announced support for .Net 6 as a native-language option for Lambda functions (previously, only .Net Core 3.1 was supported). This is exciting because the newly released Net 6.0 and C# 10 between them have some pretty cool changes that might make you rethink what you know about .Net. Let's just look at just a few that I think are pretty intriguing.

Implicit and Global Usings

You're probably familiar with C# or Typescript files with dozens of using lines at the head of the file. Not anymore - C# 10 introduce implicit and global usings. Using these two features together means you can move all of your using statements into a single, global usings file. In fact, you don't need to add system libraries to the usings file, only libraries you've added beyond the template. So even your GlobalUsings file can remain pretty tidy.

Top-Level Statements and the Minimal Hosting Model

Previous versions of C# had a lot of boilerplate in every file. In C# 9, support was introduced for top-level statements. So something like this:

using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Hello World!");
    }
}

Can now be written like this:

Console.WriteLine("Hello, World!");

That's right - a one-line Program.cs file. No Main method required! The top-level statements will definitely simplify your code, especially if you also take advantage of .Net's new, minimal hosting model. In the past, separate steps were required for setting up the host builder and configuring services. Now, these steps can all be done in a single file. Below is what the web api template generated before:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }    
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

Oof. That's a lot of boilerplate code and I haven't even added any routes or services. With top-level statements and the minimal hosting model, this empty web api project now becomes this:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();

Now, if we could just get rid of those semicolons...

But Wait, There's More!

Actually, there's a lot more - .Net 6 boasts some significant performance improvements, and combined with C# 10 there are a whole range of language improvements and improved cross-platform support. I'm still digging through the changes and looking forward to reducing the bloat in my .Net projects, and I'll write about other interesting features I find. I personally love the top-level statements and minimal hosting model because I like concise, easy to read code that doesn't force me to scroll all over the place to figure out what's going on.

References

To learn more about .Net 6 and C# 10, check out these posts:

.Net 6 Release announcement

C# 10 Release announcement