When can an entry point main be omitted?

AnotherCharles 21 Reputation points
2021-01-14T18:31:33.427+00:00

I started evaluating .NET 5 in doing some lessons in the learn tutorials.
The Tutorial added this code to the file Program.cs

I didn't find any hint, why there is no need for an entry point main.
Is this a short form?

I am happy for every hint.

using ContosoPets.Api;
using ContosoPets.Api.Data;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

CreateHostBuilder(args).Build().SeedDatabase().Run();

static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());

static class IHostExtensions
{
    public static IHost SeedDatabase(this IHost host)
    {
        var scopeFactory = host.Services.GetRequiredService<IServiceScopeFactory>();
        using var scope = scopeFactory.CreateScope();
        var context = scope.ServiceProvider.GetRequiredService<ContosoPetsContext>();

        if (context.Database.EnsureCreated())
            SeedData.Initialize(context);

        return host;
    }
}

Thanks
Charly

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,036 Reputation points
    2021-01-14T22:45:32.293+00:00

    Hello @CharlyArtmann-6633

    This is C# 9, Top-Level feature, see the following.

    Conventional

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

    Top level C#9

    using System;
    
    Console.WriteLine("Hello World!");
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful