How to use top-level programs in C# 9

When creating applications in the C# programming language, you invariably need to have to publish a large amount of boilerplate code — even for simple console applications. Imagine that you want to publish some code to check no matter if a library or an API is operating thoroughly. You may possibly publish a console software to carry out this, but you’re even so constrained to adhere to standard C# semantics. You ought to publish your code within the Key approach.

Prime-amount applications, a new thought launched in C# nine., allow for you to publish code for simple applications sans the need to have to publish boilerplate code. Prime-amount applications are a excellent new characteristic that enables you to publish cleaner, shorter, and more simple code. You can choose edge of prime-amount applications to investigate new concepts. This post discusses how you can do the job with prime-amount applications in C# nine..

To do the job with the code illustrations supplied in this post, you ought to have Visible Studio 2019 set up in your procedure. If you never previously have a duplicate, you can download Visible Studio 2019 below. Notice that C# nine. is out there in Visible Studio 2019 version sixteen.nine Preview 1 or later on, and in the .Web 5. SDK.

Make a .Web Main console software venture in Visible Studio

First off, let us build a .Web Main console software venture in Visible Studio. Assuming Visible Studio 2019 is set up in your procedure, adhere to the measures outlined under to build a new .Web Main console software venture in Visible Studio.

  1. Start the Visible Studio IDE.
  2. Click on “Create new venture.”
  3. In the “Create new project” window, pick “Console App (.Web Main)” from the record of templates displayed.
  4. Click Up coming.
  5. In the “Configure your new project” window, specify the name and locale for the new venture.
  6. Click Make.

We’ll use this venture to do the job with prime-amount applications in the subsequent sections of this post.

Prime-amount method case in point in C# nine.

Let us glimpse at a before-and-just after case in point of how prime-amount applications can eradicate boilerplate code. In advance of prime-amount statements in C# nine., this is the minimum code you’d publish for a console software:

employing Program
namespace IDG_Prime_Level_Programs_Demo

    class Method
        static void Key(string[] args)
            Console.WriteLine("Hi Earth!")

When operating with C# nine., we can stay clear of the sound and choose edge of prime-amount applications to publish our code in a considerably more simple way. The following code snippet illustrates how you can choose edge of prime-amount statements to refactor the higher than code:

employing Program
Console.WriteLine("Hi Earth!")

In possibly scenario, when the method is executed, you will see the string “Hello Earth!” displayed at the console window.

Copyright © 2021 IDG Communications, Inc.