C# 9.0: Top Level Programs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this video my name is phil beckberg and today we're gonna be talking about top level programs in c sharp nine are you new to c-sharp if you are it might be bothering you that there's no simple way of running a few lines of c-sharp code without all the boilerplate code in program.cs if you are a seasoned c-sharp developer you've probably already experimented with programs such as linkpad which allows you to achieve exactly what we're going to look at in this video being able to experiment with apis or write small algorithms to test a theory is truly handy with c-sharp 9 microsoft is introducing something called top level programs let's give this new feature a try to run this locally we've got two options we can install visual studio 2019 preview together with net 5 or we can head over to sharplab.io where we can run the code right out of the browser let's use sharplab for now to make sure that we are looking at the same things this will allow us to explore the decompiled code which means that we can look at what's generated under the hoods the first thing that we'll do is to remove all of the pre-added code in this program we're also going to make sure that we're pointing this to the master branch and you're probably wondering what this means putting this to the master branch on github where this repository lives means that we're using the c-sharp compiler that's the latest version using the latest version means that we can try out these new c-sharp nine features and other new language features that are added as well if we now add system.console.writeline hello world to the editor on the left you can see that this will now print hello world this is without any of the public static void main stuff that we normally see when we were building c-sharp apps this is truly awesome it makes it easier for us to explore the language your imagination is only limitation of what you can do at this point to illustrate that we can add a using statement for system instead of writing system.console we could also use the language feature that allows us to use all and include all the static members by adding using static system.console to the top this means that the code can now be simplified to writeline hello world you're probably wondering at this point what this looks like under the hoods if we change the right pane to decompile the source code we can see that it generated all the code that we normally see in the entry point of an application this is truly interesting and really beneficial for new as well as experienced developers let's add a class to the top level program to see what happens i'm going to introduce a class called person with a property called name keep an eye on the right pane and you'll see that it's now changing we could even create an instance of this class and print the name of a newly created person are you wondering what the benefit of this is this is great for beginners who wants to explore c-sharp without a lot of fuss and cluttered code it's also great for experienced developers that want to explore apis or try a few of these different language features you can try things like the switch expressions or other new language features in c sharp 6 7 8 and 9. you can even use async and await in this top level program i'd love to hear in the comments if this is something that you'd consider using is this a language feature that you've been wanting or do you think c-sharp has enough features as it is if you like this video please do leave a like or a comment and subscribe to the channel for more videos in the future thanks for watching this video my name is phillip ekberg and today we've been looking at top level programs in c sharp i'll see you in the next video
Info
Channel: Filip Ekberg
Views: 5,800
Rating: 4.9117646 out of 5
Keywords: csharp, programming, dotnet, .NET, SharpLab, Microsoft, Pluralsight, Filip Ekberg
Id: iGrZys6Vo_E
Channel Id: undefined
Length: 5min 18sec (318 seconds)
Published: Thu Sep 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.