C# Tips and Tricks (Part 2)

Posted 21/9/2021 by Qasim Asghar
LinkedIn ShareShare

In this second part of a 3 part series, Mohamad Lawand (https://www.mohamadlawand.com/) will be sharing C# tips and tricks you can use.

In this article we will be covering:

 

  • Top-level statements
  • Static Directive


You can watch the full video on YouTube
 


You can find the source code on GitHub:
https://github.com/mohamadlawand087/v34-CodeTipsTricks-Ep2.


Top-level statements

Top-level statements are new language features that got added in C# 9 and one of the goals is to make it easier for beginners to jump start developing C# applications.

When writing top-level statements, I don't need to add all of the boiler plates code that i usually add when writing code.



All of the boiler plates code will be automatically generated for us and we don't have to worry about creating it ourselves.

Top-level statements also support async code


Now let us introduce methods



Now let's run the application and see it in action.

The goal of top-level calls is to simplify this code and we can transform our application to something easy to read and understan

C# will assume this is the entry point to our application

Why use TLC

 

  • Simplicity
  • Quick to create
  • Very simple to use in a none windows environment
  • Easily created from within the terminal


Static Directive

Using static directive allows us to use static types inside our applications without needing to referencing the type name everytime.

One common example of this is using Console application will allow us to read/write to line. but we need to add the Console type everytime we want to use.

What if we can imply that we are using console type without having to write console everytime we use console?

Ready to find your next big challenge? Let's Go!