Top-level programs in C# 9.0
C# 9.0 comes with nice new feature called top-level programs. It’s something that teachers of beginner classes will love for sure. Imagine – you start teaching C# with only two lines of code on screen. All missing code is generated by compiler. This blog post introduces top-level programs and shows some secrets of new C# compiler.
Classic console application
Here’s the classic console application. We have seen it thousands of times and we know what it does and how it works.
using System;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
When students see this they usually have more than one question:
- What is doing “using System”?
- What is “namespace”?
- What is string “string[] args”?
Besides question there’s too much noice for them and all work on whiteboard happens inside Main() method.
What is top-level program?
With C# 9 we can skip all the noise and use top-level programs. Same code with this new feature looks like this.
using System;
Console.WriteLine("Hello World!");
Just two lines, no spacing and all the screen space is for my examples. Great!
Of course, I must have using directives but here’s the little trick to calm youngsters down.
using System; // let's talk about it later
Console.WriteLine("Hello World!");
Not only for teachers. Top-level programs can be used also in practice when writing real code. It is perfect when writing simple utility applications that doesn’t have much code.
Top-level programs are C# language feature and it doesn’t come down to Common Language Runtime (CLR). C# compiler produces Program class and Main() method (almost) like it was before.
But what about args argument of Main() method? It’s magically here and available in top-level programs. Here’s how to write out all arguments of program.
for(var i = 0; i < args.Length; i++)
{
Console.Write(i);
Console.Write(" ");
Console.WriteLine(args[i]);
}
Using methods in top-level programs
We can write methods too. Notice that methods in top-level programs must be static.
using System; // let's talk about it later
Console.WriteLine(SayHello("students"));
static string SayHello(string name)
{
return "Hello, " + name;
}
It’s interesting to see how methods in top-level programs look like functions in functional languages.
Using classes in top-level programs
We can also use classes and other things like structures, enums etc. The next code sample demonstrates primitive greeter class.
using System; // let's talk about it later
var greeter = new Greeter();
var helloTeacher = greeter.Greet("teacher");
var helloStudents = SayHello("students");
Console.WriteLine(helloTeacher);
Console.WriteLine(helloStudents);
static string SayHello(string name)
{
return "Hello, " + name;
}
public class Greeter
{
public string Greet(string name)
{
return "Hello, " + name;
}
}
In this point things may get a little bit messy as there are methods and classes coming one after another.
Order matters! In top-level programs type definitions must come after top-level statements. It means after everything that goes to Program class.
Top-level programs after compiling
Classes in top-level programs are not particularly interesting as they are compiled like usual. But things get interesting with Program class and Main() method. Here’s the decompiled code of sample with greeter class.
using System;
using System.Runtime.CompilerServices;
[CompilerGenerated]
internal static class <Program>$
{
private static void <Main>$(string[] args)
{
string str1 = new Greeter().Greet("teacher");
string str2 = <Program>$.<<Main>$>g__SayHello|0_0("students");
Console.WriteLine(str1);
Console.WriteLine(str2);
}
internal static string <<Main>$>g__SayHello|0_0(string name)
{
return "Hello, " + name;
}
}
Important things to notice:
- Program class has CompilerGenerated attribute
- Program class, Main() method and SayHello() method have obfuscated names that make it impossible to refer to them in .NET languages.
- All top-level code members are in private or internal scope and invisible to other libraries.
Of course, it doesn’t stop us using obfuscated names if we use reflection. But I really don’t see any use case where we would need it and where we really cannot use some other approach.
Wrapping up
Top-level programs is nice C# language feature that cleans up Main() method of Program class by generating class and method automatically. All we have to do is to write the code and build it. Top-level programs have different Program class and Main() method as these are compiler generated. These two are not directly accessible from other libraries although we can do everything we want with reflection. Practical use cases for top-level programs are teaching coding to beginners and writing simple utility applications.
Pingback:Dew Drop – September 3, 2020 (#3268) | Morning Dew
Pingback:The Morning Brew - Chris Alcock » The Morning Brew #3062
Is it possible to use C#9 features yet in Visual Studio/Preview?
I don’t think that this is a nice feature. In the future there will be many issues on stack overflow with people who will learn C# language and didn’t know about that generation under the hood. It’s nice feature for matured programmers but not on the beginner level. I think that it should be enabled with special attribute or something like that to prevent that type of development by default.
Its a dumb idea, if a main entry point is too confusing to someone maybe they should try learn knitting, or cleaning toilets. Making the language retard friendly just means more retarded programs. The idea that everyone can learn to code is like saying everyone can write a book. While technically true it will produce a lot of books no one wants to read.
Paul, yes you can do it with Visual Studio 2019 Preview and latest .NET 5 SDK.
Question to LordWabbit – why do you call students and n00bies retards? After getting into coding they will do big things in future. Don’t be so pessimistic :)
I’m not calling students or noobies retards, I’m saying if someone thinks that starting at the beginning (entry point) is too confusing maybe they should pick another avenue to express themselves. This whole “make things easier” paradigm has been tried before. Do you remember COBOL? BASIC? Yet here we are, clearly it didn’t work. While we are at it, why don’t we start a “build a bridge” or a “build your own house” exercise and see how that goes. All we need to do is remove all the pesky difficult stuff, like strength of materials, the law of physics, maths. What could go wrong? We could replace all those pesky designs and complications with a series of simple stencils everyone can use, they can go on to build big things as long as they are happy that everything is going to look like the stencil. Lowering the entry bar for something doesn’t mean you get more qualified people, it means you get more subpar work. BUT, we DO need more COBOL developers, perhaps try teaching that.
How do I make first person 3D Player/Camera movement? I tried for so long yet it never worked. can you help me with that please?
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
https://forum.mulegendreload.net/index.php?/gallery/image/494-13/
Your article helped me a lot, is there any more related content? Thanks!
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your article helped me a lot, is there any more related content? Thanks!
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Good https://is.gd/tpjNyL
fnunzu
iv7oxv
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
On this platform, you can discover a great variety of online slots from top providers.
Players can try out traditional machines as well as feature-packed games with stunning graphics and interactive gameplay.
Whether you’re a beginner or a seasoned gamer, there’s a game that fits your style.
money casino
All slot machines are ready to play round the clock and designed for laptops and mobile devices alike.
All games run in your browser, so you can start playing instantly.
Platform layout is easy to use, making it quick to browse the collection.
Join the fun, and discover the thrill of casino games!
jame3p
This website, you can find a great variety of slot machines from leading developers.
Users can enjoy traditional machines as well as new-generation slots with stunning graphics and exciting features.
Whether you’re a beginner or an experienced player, there’s always a slot to match your mood.
play casino
Each title are available round the clock and optimized for laptops and tablets alike.
No download is required, so you can start playing instantly.
Site navigation is user-friendly, making it simple to explore new games.
Register now, and dive into the thrill of casino games!
Here, you can access lots of slot machines from leading developers.
Players can enjoy traditional machines as well as modern video slots with stunning graphics and bonus rounds.
Even if you’re new or a seasoned gamer, there’s always a slot to match your mood.
slot casino
All slot machines are ready to play round the clock and compatible with PCs and smartphones alike.
All games run in your browser, so you can get started without hassle.
Site navigation is easy to use, making it quick to browse the collection.
Register now, and discover the world of online slots!
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
u7kz7f
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
1xBet Promotional Code – Exclusive Bonus up to $130
Apply the 1XBet promotional code: 1xbro200 while signing up via the application to unlock exclusive rewards provided by 1xBet and get 130 Euros maximum of a full hundred percent, for sports betting and a €1950 with one hundred fifty free spins. Open the app and proceed with the registration procedure.
The 1xBet promo code: 1xbro200 offers an amazing welcome bonus for new users — full one hundred percent maximum of €130 during sign-up. Bonus codes serve as the key to obtaining rewards, also One X Bet’s promotional codes aren’t different. When applying such a code, users can take advantage of several promotions in various phases within their betting activity. Although you aren’t entitled for the welcome bonus, 1XBet India makes sure its regular customers receive gifts via ongoing deals. Visit the Offers page on their website frequently to remain aware about current deals meant for current users.
1xbet promo code india today
What 1XBet promo code is currently active at this moment?
The promotional code applicable to 1XBet is Code 1XBRO200, permitting novice players registering with the bookmaker to gain an offer of $130. In order to unlock special rewards pertaining to gaming and sports betting, please input the promotional code related to 1XBET in the registration form. To make use from this deal, potential customers must input the promotional code 1xbet while signing up procedure to receive a full hundred percent extra on their initial deposit.
1lcgoz
erpl5q
1
qwhkbd
2318ii
cislaf
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://accounts.binance.com/pt-BR/register-person?ref=YY80CKRN
qo9q42
kv4vod
842vk0
Your article helped me a lot, is there any more related content? Thanks!
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Hello Neat post Theres an issue together with your site in internet explorer would check this IE still is the marketplace chief and a large element of other folks will leave out your magnificent writing due to this problem
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your article helped me a lot, is there any more related content? Thanks!
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
dzqm9j
Your article helped me a lot, is there any more related content? Thanks!
Your article helped me a lot, is there any more related content? Thanks!