C# A to Z: Prelude

Shahed Chowdhuri @ Microsoft - Dec 28 '20 - - Dev Community

Update: Due to new personal commitments and more work commitments in 2021, I wasn't able to make much progress with my weekly C# A-Z series.

For now, I'll focus on some new content for my regular blog (WakeUpAndCode.com) and hope to revisit the A-Z series with .NET 6.

Original Post:

Introduction

To kick off 2021, let's celebrate .NET 5 by exploring 26 C# topics from A to Z. This upcoming series won't be a comprehensive guide or an exhaustive list, but will highlight several useful C# features, primarily focusing on C# 9. We will also cover a few other topics from v7 and v8 and some earlier versions as well.

The goal of this A-Z series is to explain new/recent language features to existing .NET developers, while introducing the language to new developers as well. Each blog post will compare some before/after scenarios to demonstrate how you would write the program without a specific feature, and then illustrate how that feature can be used.

For a cleaner approach, code samples will use a "Top Level Program" format, which was recently introduced in C# 9 with .NET 5.

Prerequisites

Before you get started, please install the latest version of .NET and your preferred IDE or code editor, e.g. Visual Studio 2019 or Visual Studio Code.

Without Top-Level Programs

Without using a top-level program, here's what a C# program would typically look like.

using System;

namespace azprelude
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World from .NET Core 3.1!");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

In the above program, you can see the following:

  1. using statement at the top to use the System namespace
  2. current namespace for this program, i.e. azprelude
  3. name of class, e.g. Program
  4. entry point, e.g. static Main method with no (void) return type
  5. string array of arguments, e.g. string[] args
  6. simple statement to print some text using System.Console.WriteLine()

Creating a New Project

To create the above Console Application, you may choose one of the following options:

Option A:

In Visual Studio 2019, click File | New | Project and then select the Console App (.NET Core) project template

VS2019 New Project

Option B:
In Visual Studio Code, type in the following command in the integrated Terminal (which also works in any command line)

VS Code Terminal

dotnet new console -o azprelude
Enter fullscreen mode Exit fullscreen mode

Optionally, you can also specify the target framework with the -f flag. For .NET 5, use net5.0 and for .NET Core 3.1, use netcoreapp3.1.

dotnet new console -o azprelude-net5 -f net5.0
Enter fullscreen mode Exit fullscreen mode
dotnet new console -o azprelude-netcore31 -f netcoreapp3.1
Enter fullscreen mode Exit fullscreen mode

Using Top-Level Programs

Using a top-level program considerably simplifies the code, as shown below:

using System;

Console.WriteLine("Hello World from .NET 5 using C#9!");
Enter fullscreen mode Exit fullscreen mode

Or better yet, this can be further shrunk down by referring to System.Console.WriteLine() in a single statement.

System.Console.WriteLine("Hello World from .NET 5 using C#9!");
Enter fullscreen mode Exit fullscreen mode

Behind the scenes, the compiler will still generate the necessary Program class and Main method, so both programs will behave the same way. To get access to the arguments in the Main method, simply refer to the arguments with an array index, e.g. args[0], args[1], etc.

using System;

Console.WriteLine("Hello World from .NET 5 using C#9!");

var age = args[0];
var title = args[1];
var idnumber = args[2];

Console.WriteLine($"The age is {age}.");
Console.WriteLine($"The title is {title}.");
Console.WriteLine($"The idnumber is {idnumber}.");
Enter fullscreen mode Exit fullscreen mode

Setting Argument Values

In the Terminal, it's pretty easy to set the argument values in their proper numeric order.

dotnet run 23 "Software Engineer"  12345
Enter fullscreen mode Exit fullscreen mode

This will allow you to run the program using "dotnet run" followed by any text/numeric values for program arguments.

VS Code args

However, you may be wondering how you can pass in command line arguments from Visual Studio 2019 while debugging. Simply follow the steps below:

  1. In Solution Explorer, right-click your project.
  2. Select Properties to view/edit project properties.
  3. Click the Debug tab.
  4. Add the following values in the "Application arguments" field: 23 "Software Engineer" 12345

VS2019 args

The end result will still be the same when you run the program in Visual Studio 2019.

VS2019 args output

Behind the scenes, Visual Studio 2019 will update launchsettings.json to include the command line arguments you added.

{
  "profiles": {
    "azprelude-net5": {
      "commandName": "Project",
      "commandLineArgs": "23 \"Software Engineer\"  12345"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

TIP: You may also add/edit/delete values directly in this file. Please note that arguments enclosed in quotation marks must be escaped with a backslash for each quotation mark.

Conclusion

After 20+ years in existence, the C# Programming language has come a long way. As more features are added from one release to the next, many fans have come to love and appreciate the language even more. However, some people may feel that the language is being overly complex and may not be friendly for beginners.

To build upon existing features, introduce new features and keep things simple for newcomers, C# can now take advantage of top-level programs. Just make sure that you only add one (and only one) top-level program per project. Otherwise, the compiler won't be able to identify the entrypoint for your program.

What's Next

This blog post gave you a taste of what's to come next. Stay tuned for 26 different topics in the weeks and months ahead!

Up next:

  • A is for: Assignment with Init-Only Setters
  • B is for: TBA
  • C is for: TBA
  • D is for: TBA
  • E is for: TBA
  • F is for: TBA
  • G is for: TBA
  • H is for: TBA
  • I is for: TBA
  • J is for: TBA
  • K is for: TBA
  • L is for: TBA
  • M is for: TBA
  • N is for: TBA
  • O is for: TBA
  • P is for: TBA
  • Q is for: TBA
  • R is for: TBA
  • S is for: TBA
  • T is for: TBA
  • U is for: TBA
  • V is for: TBA
  • W is for: TBA
  • X is for: TBA
  • Y is for: TBA
  • Z is for: TBA

References

All future code samples will be added to the existing GitHub repository:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .