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.
- Start the Visible Studio IDE.
- Click on “Create new venture.”
- In the “Create new project” window, pick “Console App (.Web Main)” from the record of templates displayed.
- Click Up coming.
- In the “Configure your new project” window, specify the name and locale for the new venture.
- 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.
Use procedures in prime-amount applications in C# nine.
You can use procedures with prime-amount applications. Down below is a code case in point that illustrates how you can use procedures with prime-amount applications.
Program.Console.WriteLine(DisplayMessage("Joydip!"))
Program.Console.Examine()
static string DisplayMessage(string name)
return "Hi, " + name
When you execute the higher than method, you ought to see the output “Hello, Joydip!” show up in the console window:
Use classes in prime-amount applications in C# nine.
You can also use classes, structs, and enums in prime-amount applications. The following code snippet illustrates how you can use classes in prime-amount applications.
Program.Console.WriteLine(new Author().DisplayMessage("Joydip!"))
Program.Console.Examine()
community class Author
community string DisplayMessage(string name)
return "Hi, " + name
When you execute the higher than method, the output will be equivalent to Figure 1.
How prime-amount applications do the job in C# nine.
So, how do prime-amount applications do the job particularly? What comes about driving the scenes? Prime-amount applications are effectively a compiler characteristic. If you never publish the boilerplate code, the compiler will produce it for you.
Refer to the following piece code we wrote before.
employing Program
Console.WriteLine("Hi Earth!")
The code displayed under (produced employing the SharpLab on the internet tool) displays what the compiler-produced code would glimpse like.
employing Program
employing Program.Diagnostics
employing Program.Reflection
employing Program.Runtime.CompilerServices
employing Program.Protection
employing Program.Protection.Permissions
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = real)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = real)]
[assembly: AssemblyVersion("...")]
[module: UnverifiableCode]
[CompilerGenerated]
inside static class$
personal static void$(string[] args)
Console.WriteLine("Hi Earth!")
If you glimpse at the compiler-produced code, you will see the [CompilerGenerated] attribute on prime of the static class produced by the compiler.
Prime-amount applications are a excellent new characteristic in C# nine., whereby the compiler mechanically generates the boilerplate code for you driving the scenes. Prime-amount applications are excellent for simple applications that never have way too lots of files and dependencies. Notice that only one file in your software may use prime-amount statements otherwise the compiler throws an mistake.
One downside to prime-amount applications is that, if you’re new to C#, you may possibly not be equipped to understand what is happening in the code driving the scenes. A much better way for novices to study C# will be employing the Key approach, and keeping away from prime-amount statements until you understand how the Key approach will work. But people who have mastered Key will find prime-amount statements a incredibly beneficial shortcut.
How to do additional in C#:
Copyright © 2021 IDG Communications, Inc.