Top Level Statement in C# 9

Hey folks, I'm coming along to tell you a few things about the new feature of C# 9. Every time you want to start a new project, you always have the same Main File, with the same structure. At the next version of C# (version 9), we'll have a new feature called Top Level Statement. With that, we'll be able to code our app without any namespace/class/public void static main structure. Take a look at the code below.

using System;

Console.WriteLine("Hello World!");

It looks pretty simple, but with this, you can play around with your code, and even code a simple/tiny Web API. To start playing with the new features, firstly download the .net 5 SDK and change your .csproj file to target the .net 5 and language preview like bellow.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>
</Project>

And you're ready to play with the awesome features available in the preview. Let's code a way to get the image of the day from NASA API and print the Picture of the Day. No awesome code here, it's really a way to show how simple a simple call could be. After here, why not save the image in a file? Maybe send the file over telegram?

using System;
using System.Net.Http;
using System.Text.Json;

var API = "https://api.nasa.gov/planetary/apod?hd=true&api_key=DEMO_KEY";
var cli = new HttpClient();

var response = await cli.GetAsync(API);
var data = JsonSerializer.Deserialize<NasaApi>(await response.Content.ReadAsStringAsync());

Console.WriteLine(data.Url);

That's it for today folks. Keep an eye open for the awesome features C# 9 will bring to us, there's plenty more features already available.


Translations:




Comments

comments powered by Disqus

Newsletter

Sua assinatura não pôde ser validada.
You subscribed successfully!

Newsletter

Subscribe for Newsletter