How Hello World! changed – top level statements and functions (C# 9)

C# 9 comes with many new features to enhance productivity and fix bugs. One productivity enhancement comes for small programs and learning C#: top level statements. This also allows for a new way to create a Hello World program with C#. It’s not the first time that a new C# language feature made a change for Hello World. This also happened with C# 6. Let’s come on a tour how Hello World changed during the C# years, and what’s going on with top level statements and functions.

Hello World

C# 1

The first version of C# was influenced by Java, Delphi, and C++. To create a simple Hello World application, the Main method had to be defined as the entry point into the application. With this object-oriented programming language every method had to be put into a type, e.g. the Main method into the Program class. To write a string to the console, the WriteLine method of the Console class can be used. The Console class is defined within the System namespace, so the using declation openend this namespace:

using System;

public class Program
{
  public static void Main()
  {
    Console.WriteLine("Hello World!");
  }
}

C# 6 – Using Static Directive

Following the different versions of C#, every new version offers many new features – just a few mentioned here are generics, LINQ, and async/await. The Hello World application didn’t change – up to C# 6. With C# 6 using static was added which allows to open all members of a static type. It’s no longer needed to use the class name when invoking a static method. The first time with C# the Hello World application was changed. The WriteLine method can be invoked without the class name:

using static System.Console;

public class Program
{
  public static void Main()
  {
    WriteLine("Hello World!");
  }
}

C# 9 – Top-Level Statements

It took some years for the next simplification of Hello World. With C# 9 Top-level statements, it’s no longer necessary to delcare the Main method at all. Just add method invocations top-level.

using System;

Console.WriteLine("Hello World!");

Behind the scenes, the compiler creates a $Program class and a $Main method:

public class $Program
{
  public static void $Main()
  {
    System.Console.WriteLine("Hello World!");
  }
}

#csharp #csharp9 #function

What is GEEK

Buddha Community

How Hello World! changed – top level statements and functions (C# 9)

How Hello World! changed – top level statements and functions (C# 9)

C# 9 comes with many new features to enhance productivity and fix bugs. One productivity enhancement comes for small programs and learning C#: top level statements. This also allows for a new way to create a Hello World program with C#. It’s not the first time that a new C# language feature made a change for Hello World. This also happened with C# 6. Let’s come on a tour how Hello World changed during the C# years, and what’s going on with top level statements and functions.

Hello World

C# 1

The first version of C# was influenced by Java, Delphi, and C++. To create a simple Hello World application, the Main method had to be defined as the entry point into the application. With this object-oriented programming language every method had to be put into a type, e.g. the Main method into the Program class. To write a string to the console, the WriteLine method of the Console class can be used. The Console class is defined within the System namespace, so the using declation openend this namespace:

using System;

public class Program
{
  public static void Main()
  {
    Console.WriteLine("Hello World!");
  }
}

C# 6 – Using Static Directive

Following the different versions of C#, every new version offers many new features – just a few mentioned here are generics, LINQ, and async/await. The Hello World application didn’t change – up to C# 6. With C# 6 using static was added which allows to open all members of a static type. It’s no longer needed to use the class name when invoking a static method. The first time with C# the Hello World application was changed. The WriteLine method can be invoked without the class name:

using static System.Console;

public class Program
{
  public static void Main()
  {
    WriteLine("Hello World!");
  }
}

C# 9 – Top-Level Statements

It took some years for the next simplification of Hello World. With C# 9 Top-level statements, it’s no longer necessary to delcare the Main method at all. Just add method invocations top-level.

using System;

Console.WriteLine("Hello World!");

Behind the scenes, the compiler creates a $Program class and a $Main method:

public class $Program
{
  public static void $Main()
  {
    System.Console.WriteLine("Hello World!");
  }
}

#csharp #csharp9 #function

Loma  Baumbach

Loma Baumbach

1603381251

Introducing C# 9: Static anonymous functions

Introduction

C## 9 brings an important improvement to anonymous functions by allowing the modifier static on them and we now have static anonymous functions ! Why Microsoft brought this feature ? Because allocation matters ! Microsoft explains here that lambda are not cost less (https://devblogs.microsoft.com/premier-developer/dissecting-the-local-functions-in-c-7/) :

“Anonymous methods are not cheap:”

  • Overhead of a delegate invocation (very very small, but it does exist).
  • 2 heap allocations_ if a lambda captures local variable or argument of enclosing method (one for closure instance and another one for a delegate itself)._
  • 1 heap allocation_ if a lambda captures an enclosing instance state (just a delegate allocation)._
  • 0 heap allocations_ only if a lambda does not capture anything or captures a static state._

#c# #.net 5 #c# 9 #static anonymous function #c++

Let's Give Some Unit Testing Love to C# 8 and C# 9 Features

According to StackOverflow, C## is one of the most-loved programming languages. And I completely understand that—it is powerful, easy to learn and consistently improving and developing. It is a living language. :)

The last couple of years, there were new features added to the languages, and the new versions keep coming up—C## 7, C## 8, C## 9.

As you know, we at Progress Telerik are proud that our products are always in sync with the latest things in the .NET world, and C## 9 and JustMock are no exception.

#c #c# #c#8 #c#9

Abdullah  Kozey

Abdullah Kozey

1624100940

ICYMI C# 9 New Features: Top-level Statements

This is the first in a series of articles on new features introduced in C#9.

Top-level statements allow you to simplify and remove some of the “ceremony” in your code.

For example, take the following console application written in C#8:

?

using System.Linq;

using static System.Console;

namespace ConsoleAppCS8

{

class Program

{

static int Main(string[] args)

{

string greeting = ""``;

if (args.Any())

{

greeting = args[0];

}

WriteLine(``"Please enter your name"``);

var name = ReadLine();

var upperName = ConvertToUpper(name);

WriteLine($``"{greeting} {upperName}"``);

return 42;

}

public static object ConvertToUpper(string name)

{

return name.ToUpperInvariant();

}

}

}

In the preceding code the “ceremony” consists of things such as the enclosing namespace, the Program class outline, and the Main method itself.

With top-level statements in C## 9 this code can be simplified to the following:

?

using System.Linq;

using static System.Console;

string greeting = ""``;

if (args.Any())

{

greeting = args[0];

}

WriteLine(``"Please enter your name"``);

var name = ReadLine();

var upperName = ConvertToUpper(name);

WriteLine($``"{greeting} {upperName}"``);

return 42;

static object ConvertToUpper(string name)

{

return name.ToUpperInvariant();

}

Notice in the C## 9 version that the structure is a lot “flatter” because there are no nested {} from the namespace, class, and Main method.

The application will still act in the same way, there is just less boilerplate code.

#icymi c# 9 #c# 9 #new features

Shaylee  Lemke

Shaylee Lemke

1589796000

C++ Tutorials - If Else Statement In C++

In this C++ Tutorial we are going to talk about If Else Statement in C++ . using conditional statement we can execute some section of the code according to a condition. and particularly in this article we make some examples of if else condition. using if statement you can control if a program enters a section of code or not based on whether a given condition is true or false.

Youtube channel: Parwiz Forogh - https://www.youtube.com/watch?v=R4UBRRUMbbE

#c #c# #c++ #programming-c