The C# programming language can be used to build software components and applications that run on a wide variety of operating systems (platforms)—including mobile devices, game consoles, web applications, Internet of Things (IoT), microservices, and desktop applications. Furthermore, C# is free; in fact, it is entirely open source, so you can view, modify, redistribute, and contribute back any improvements you make. As a language, C# is built on features found in its predecessor C-style languages (C, C++, and Java), making it immediately familiar to many experienced programmers.1
This chapter introduces C# using the traditional HelloWorld program. It focuses on C# syntax fundamentals, including defining an entry point into the C# program, which will familiarize you with the C# syntax style and structure and enable you to produce the simplest of C# programs. Prior to the discussion of C# syntax fundamentals is a summary of managed execution context, which explains how a C# program executes at runtime. This chapter ends with a discussion of variable declaration, writing and retrieving data from the console, and the basics of commenting code in C#.
The best way to learn a new programming language is to write code. The first example is the classic HelloWorld program. In this program, you will display some text to the screen.
Listing 1.1 shows the complete HelloWorld program; in the following sections, you will compile and run the code.
(A listing this simple requires a feature – top-level statements – enabled in C# 9.0 to help with learning C#. An alternative listing, arguably a more typical listing, is shown later in the Listing 1.6 . See Chapter 4 for more information about top-level statements.)
Once you have written your C# code, it is time to compile and run it. Based on your operating system, you have a choice of which compiler to download. Generally, the implementation is packaged into a software development kit (SDK). The SDK includes the compiler, the runtime execution engine, the framework of pragmatically accessible functionality that the runtime can access (see “Application Programming Interface” later in the chapter), and any added tooling (such as a build engine for automating build steps) that might be bundled with the SDK. Given that there are compilers available for multiple platforms and the fact that C# has been publicly available since 2000 (see “Multiple .NET Frameworks” later in the chapter), there are several options to choose from.
For each operating system the installation instructions vary. For this reason, I recommend you visit https://dotnet.microsoft.com/download for download and installation instructions, selecting the package to download based on which operating system you will be developing on. Furthermore, you have a choice of which .NET implementation(s) to use—sometimes referred to as the .NET framework(s). While I could provide further details here, the .NET download site has the most updated instructions for each combination supported. While there are multiple framework versions available, the easiest is to download the default from that URL, which will correspond to the latest fully released version.
There are also numerous ways to edit your source code, including the most rudimentary of tools, such as Notepad on Windows, TextEdit on Mac/macOS, or vi on Linux. However, you’re likely to want something more advanced so that at least your code is colorized. Any programming editor that supports C# will suffice. If developing on Linux, I recommend you consider the open-source editor Visual Studio Code ( https://code.visualstudio.com ). To optimize working with C# in Visual Studio Code, you will want to install the C# extension as shown in Figure 1.1. However, if you are working on Windows or Mac, consider the corresponding version of Microsoft Visual Studio 2022 (or later)—see https://visualstudio.microsoft.com/ . All are available free of charge.
In the remainder of this section, I provide instructions for these editors. For Visual Studio Code, we rely on the command-line interface (CLI) tool dotnet CLI for creating the initial C# program in addition to compiling and running the program. For Windows and Mac, we focus on using the platform specific version of Visual Studio 2022.
The dotnet command, dotnet, is the dotnet command-line interface, or dotnet CLI, and it may be used to generate the initial code base for a C# program in addition to compiling and running the program.3 (To avoid ambiguity between CLI referring to the Common Language Infrastructure or the command-line interface, throughout the book I will prefix CLI with dotnet when referring to the dotnet CLI. CLI without the dotnet prefix refers to Common Language Infrastructure.) Once you have completed the installation, verify that dotnet is an available command from the terminal—thus verifying your installation.
Following are the instructions for creating, compiling, and executing the HelloWorld program from a terminal on Windows, macOS, or Linux:
Output 1.1 shows the output following the preceding steps.5
With Visual Studio 2022, which only runs on Microsoft Windows, the procedure is similar, but instead of using the command line, you use an integrated development environment (IDE), which has menus you can choose from rather than executing everything from the command line:
Whether using dotnet CLI or Visual Studio, there are several files created. The first file is a C# file with the name Program.cs by convention. The name Program is commonly used as the starting point for a console program, even though any name could be used. The .cs extension is the standard convention for all C# files and what the compiler expects to compile into the final program by default. To use the code shown in Listing 1.1, open the Program.cs file and replace its content with Listing 1.1:. Before saving the updated file, observe that the only functional difference between Listing 1.1: and what was generated by default is the text between the double quotes.
A configuration file called a project file is included as part of the generated source code of your C# project. The project file content will vary from one application type and .NET framework to the next. However, at a minimum, it generally identifies what application type to build (console, web, library, etc.), which .NET framework(s) to support, compiler settings, and which potential settings are needed to launch the application, along with other dependencies the code may rely on (called libraries). For example, the simple .NET console application project file created in the previous section appears in Listing 1.2.
In Listing 1.2, the application type is identified as a .NET 7.0 (net7.0) console application (Exe). The two other elements Nullable (C# 8.0) and ImplicitUsings (C# 10.0) will be discussed in Chapter 3 – Declaring Types That Allow Null and Chapter 5 – Using Directives respectively. All other settings (such as which C# files to compile) are identified by convention. For example, by default, all *.cs files in the same directory (or subdirectory) as the project file are included in the compile.
When you execute dotnet run, it first implicitly runs dotnet build to compile the code. The compiled output created by the dotnet build command is an assembly called HelloWorld.dll.6 The extension stands for dynamic link library (DLL), and with .NET, all assemblies have a .dll extension even if they are console programs, as this one is. (On Windows a .exe file is also created.) By default, the compiled output for a .NET application is placed into a subdirectory according to the TargetFramework identified in the project file – Listing 1.2. (./bin/Debug/net7.0/). The Debug directory is used because the default configuration is debug. This configuration causes the output to be optimized for debugging rather than for performance. The compiled output will not execute on its own. Rather, it needs the CLI to host the code. For .NET applications, this requires using the dotnet.exe (dotnet on Linux and Mac) process as a host process for the application. Hence why the program is executed with the dotnet run command. That said, there is a way to generate a stand-alone executable that includes the necessary runtime files so that installing the dotnet runtime is not required—see the Advanced Topic: Publishing a Stand-Alone Executable. (On Windows the .exe file can be used directly, if the dotnet runtime is installed, rather than using dotnet run.)
Both the source code and the manuscript text are available at https://EssentialCSharp.com . Alternatively, the source code for this book is available for download at on GitHub directly at https://github.com/IntelliTect/EssentialCSharp . Instructions for compiling and running the code are available in the README.md file at the same location.
An alternative approach is to paste the source code into the HelloWorld program created earlier in the chapter and then execute the source code.