Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# .NET Project Templates: Use explicit type instead of var #7938

Open
jaliyaudagedara opened this issue May 6, 2024 · 0 comments
Open

C# .NET Project Templates: Use explicit type instead of var #7938

jaliyaudagedara opened this issue May 6, 2024 · 0 comments

Comments

@jaliyaudagedara
Copy link

jaliyaudagedara commented May 6, 2024

I am sorry if this is not the place to bring this up.

All the different C# .NET project templates (ASP.NET Core, Azure Functions etc.) creates templated code with incorrect use of var.

Official .NET Coding Conventions nicely explains when to use var.

But we don't seem to follow that on C# .NET project templates.

For an example, consider the following examples.

  1. ASP.NET Core Web API template.
// We can't determine what type the builder is by looking at right side
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Same here
var app = builder.Build();

// code emitted for brevity
  1. Azure Function Template
// We can't determine what type the host is by looking at right side
var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .Build();

// code emitted for brevity
  1. Worker Service
// What's the type of the builder
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();

// Same
var host = builder.Build();

Describe the solution you'd like.

I am suggesting we follow the standard C# coding conventions.

  1. ASP.NET Core Web API template.
// It's more readable
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// It's more readable
WebApplication app = builder.Build();
  1. Azure Function Template
// It's more readable
IHost host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
    })
    .Build();
  1. Worker Service
// It's more readable
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();

// It's more readable
IHost host = builder.Build();

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant