Connect with us

Uncategorized

C# Top-Level Statements

After a break in coding with C#, I cranked up a new C# project in my Visual Studio. And I noticed a new feature in C#9, “Top-Level Statements“.

Usually, when you start a C, C++, C#, or Java program, you will have the “static main” method. But with this latest feature, you don’t need to declare the main method.

It may look like a very simple change at first, but I feel this is an important change. Let me explain.

Note: I am just sharing a thought which got me excited, and not comparing C# to JavaScript.

1. Easy to explain to Students

Sometimes, I have faced a hard time explaining why we need the main method and the concept of entry points.

This feature makes it simple to explain. You feed a list of statements to the computer and it executes it. Your code may get complex, so you need functions, classes, and packages to organize your code. Straight forward to understand.

# Program.cs

using System;

System.Console.WriteLine("Welcome to C#!");

int Add(int a, int b)
{
    return a + b;
}

int Multiply(int a, int b)
{
    return a * b;
}

void Print(string str)
{
    Console.WriteLine(str);
}

Print("Addition Result:" + Add(50, 50));
Print("Addition Result:" + Multiply(50, 50));

2. Moving to C# from JavaScript

Having worked on NodeJs, with this feature, a C# program just looked like a NodeJs program.

The execution begins at the first line, and you can have classes and modules to organize your code.

NodeJS uses V8 JavaScript Engine under the hood, which compiles JS code into machine code. With .net, you compile the program and generate the CLR code.

3. You can call async functions

If you are coming from NodeJs, you know that “await” can be used only inside an async function.

With C#, you can call await function directly in the Top-Level statements.

// C#

using System;
using System.Threading.Tasks;

Console.Write("Hello ");
await Task.Delay(5000);
Console.WriteLine("World!");
// JavaScript 

async function wait() {
    return Promise.resolve(5)
}

// This line will throw error. Comment this line.
await wait()

wait()
    .then((result) => {
        console.log(result)
    });

// Error
% node a.js
cli/test.js:5
await wait()
^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules

What is missing?

I expected a single command that will compile and execute the program. But unfortunately, there is no option to run just a single cs file. You need to first compile the program, generate an executable and then run the executable.

To compile and generate executables, you need to set up a Project, at which point, it becomes an additional cognitive load for the newbies.

IMO, NodeJs is the best language for a beginner to understand pragramming. C/C++ should be introduced next.


Continue Reading

Trending

Copyright © 2021 Rajan Panneer Selvam. Some of the content is derived from publically available information. For some of the resources I have obtained commercial licenses and you cannot use them in your projects. Before reusing any of the site content, please double-check for copyright issues. I am not responsible if you are infringing copyrights.