Why is Program.cs no longer a class? [closed]
Asked Answered
W

2

11

When you create a new application with the latest .NET Framework, Program.cs looks 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();

If you're wondering - this is literally the entire file. No public class Program; no includes; no constructors. Back in "the day" this all used to be included within a Main function of a class called Program, like so:

public class Program
{
    public async static Task Main(string[] args)
    {
        var host = CreateHostBuilder(args).Build();

        ...

        await host.RunAsync();
    }

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

So why the change to this scripting style format with no class definition for Program.cs?

Wavy answered 27/4, 2022 at 2:26 Comment(2)
B
12

Actually, it doesn't happen in .NET framework. This new syntax was released with .NET 6 and C# 9. It's called top-level statements and aims to enable you to start coding quickly, excluding the need to include repetitive ceremonial code. It's not very useful, this feature only simplifies what's needed to start coding.

Docs with more details: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements

Booze answered 27/4, 2022 at 2:44 Comment(9)
"excluding the need to include repetitive ceremonial code." - except for all the others (copy-constructors, WPF viewmodels, etc) - and it introduces the tedium of needing to re-wrap top-level code back into a normal class if you need to customize it at all - so I'm not convinced of it saving anyone much time in the long-term or for nontrivial projects.Jerryjerrybuild
@Jerryjerrybuild "if you need to customize it at all" which is pretty much every project. I share your stance completely and am baffled by these changes. It's also distracting because you expect to see a class, not a script. (I know its not literally a script but it smells like one)Wavy
@Wavy And you have to love their workaround if you don't like it: "While a .NET 6 console app template generates the new style of top-level statements programs, using .NET 5 doesn't. By creating a .NET 5 project, you'll receive the old program style. Then, you can edit the project file to target .NET 6 but retain the old program style for the Program.cs file."Shull
@Jerryjerrybuild I added a link on the main question, but it seems to be something that is being discussed on GitHub.Shull
Don't confuse .NET 6 and C# 9.0. This is a C# 9.0 feature. You can target .NET Framework 2.0 if you want (and maybe 1.0, but I've only confirmed it as far down as .NET Framework 2.0), as long as you have the language version set to 9.0 or higher, since there's nothing in the runtime that's needed to support this feature. Microsoft won't provide technical support for it, but it works.Electoral
I wouldn't mind if a new "Hello world" top level statements template was created. But changing every single example in the docs, and removing the previous templates .... judging from the number of SO questions, it's added too much confusion.Rosariarosario
Not sure what the objections are here. If you are in the habit of putting all of your business logic in the Main() method, you might be confused. However, if you are in the habit of creating classes (in separate files) to put your business logic in and then using Program.cs to bootstrap those classes, this is not a major design shift. I would argue that you are expecting your Program.cs file to do to much if you have any worries about this change.Andvari
@Andvari I think the confusion is pretty obvious. You have the entirety of a program written as object oriented within classes, and then you have a singular C# file that every developer is going to look at called ‘Program.CS’ that suddenly looks like a script. If you think that gives not a single person distracting pause the first time they see it, both experienced and new, you’re ignoring the way our brains establish and prefer consistent patterns.Wavy
What's interesting is seeing how VS decides where to start that app. I found out by adding a new class, deleting all of the 'ceremonial code' of namespace/class/etc., then putting in some 'top level statements'. VS quickly informed me that, ahem, only ONE module can contain top-level statements. Fixing stuff that isn't broken...Zales
E
0

I think the use of top-level statements came from the fact of many developers using LinqPad for the same kind of functionality. Many test code with LinqPad, then incorporate into a solution. Top-level statements cuts out the middleman.

Ellon answered 29/5 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.