C# Syntax =>

S Chapman 82 41 Reputation points
2021-01-20T17:06:20.243+00:00

PLEASE can somebody rewrite the below code for me without using the => operator. I'm trying to understand what it is doing and struggling. Have spent a lot of time but still can't get it. Thanks in advance:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,403 questions
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,306 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2021-01-20T17:30:55.84+00:00

    To replace the first “=>”, move the caret to this location and press <Ctrl+.>, then select “Use block body for methods”. The result will be probably this:

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

    To replace the second “=>” too, try something like this:

    public static IHostBuilder CreateHostBuilder( string[] args )
    {
     return Host.CreateDefaultBuilder( args )
     .ConfigureWebHostDefaults( delegate( IWebHostBuilder webBuilder)
     {
     return webBuilder.UseStartup<Startup>( );
     } );
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ken Tucker 5,846 Reputation points
    2021-01-20T17:33:12.157+00:00

    The method ConfigureWebHostDefaults is expecting an action. So basically webBuilder is an instance of IHostBuilder which you use to setup the defaults for the web host. In this case it is setting the startup class.

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilderextensions.usestartup?view=aspnetcore-5.0#Microsoft_AspNetCore_Hosting_WebHostBuilderExtensions_UseStartup__1_Microsoft_AspNetCore_Hosting_IWebHostBuilder_

    0 comments No comments