Hello, C#! Welcome, .NET!
In this first chapter, the goals are setting up your development environment, understanding the similarities and differences between .NET 5, .NET Core, .NET Framework, and .NET Standard, and then creating the simplest application possible with C# 9 and .NET 5 using Microsoft's Visual Studio Code.
After this first chapter, this book can be divided into three parts: first, the grammar and vocabulary of the C# language; second, the types available in .NET for building app features; and third, examples of common cross-platform apps you can build using C# and .NET.
Most people learn complex topics best by imitation and repetition rather than reading a detailed explanation of the theory; therefore, I will not overload you with detailed explanations of every step throughout this book. The idea is to get you to write some code, build an application from that code, and then for you to see it run.
You don't need to know all the nitty-gritty...
Setting up your development environment
Before you start programming, you'll need a code editor for C#. Microsoft has a family of code editors and Integrated Development Environments (IDEs), which include:
- Visual Studio Code
- GitHub Codespaces
- Visual Studio 2019
- Visual Studio 2019 for Mac
Using Visual Studio Code for cross-platform development
The most modern and lightweight code editor to choose, and the only one from Microsoft that is cross-platform, is Microsoft Visual Studio Code. It is able to run on all common operating systems, including Windows, macOS, and many varieties of Linux, including Red Hat Enterprise Linux (RHEL) and Ubuntu.
Visual Studio Code is a good choice for modern cross-platform development because it has an extensive and growing set of extensions to support many languages beyond C#, and being cross-platform and lightweight it can be installed on all platforms that your apps will be deployed to for quick bug...
Understanding .NET
.NET 5, .NET Framework, .NET Core, and Xamarin are related and overlapping platforms for developers used to build applications and services. In this section, I'm going to introduce you to each of these .NET concepts.
Understanding .NET Framework
.NET Framework is a development platform that includes a Common Language Runtime (CLR), which manages the execution of code, and a Base Class Library (BCL), which provides a rich library of classes to build applications from. Microsoft originally designed .NET Framework to have the possibility of being cross-platform, but Microsoft put their implementation effort into making it work best with Windows.
Since .NET Framework 4.5.2 it has been an official component of the Windows operating system. .NET Framework is installed on over one billion computers so it must change as little as possible. Even bug fixes can cause problems, so it is updated infrequently.
All of the apps on a computer written for .NET...
Building console apps using Visual Studio Code
The goal of this section is to showcase how to build a console app using Visual Studio Code. Both the instructions and screenshots in this section are for macOS, but the same actions will work with Visual Studio Code on Windows and Linux variants.
The main differences will be native command-line actions such as deleting a file: both the command and the path are likely to be different on Windows or macOS and Linux. Luckily, the dotnet
command-line tool will be identical on all platforms.
Writing code using Visual Studio Code
Let's get started writing code!
- Start Visual Studio Code.
- On macOS, navigate to File | Open.... On Windows, navigate to File | Open Folder…. On both OSes, you can click the Open Folder button in the EXPLORER pane or click the Open folder… link on the Welcome tab, as shown in the following screenshot:
Figure 1.4: The Visual Studio Code Welcome tab
...
Downloading solution code from the GitHub repository
Git is a commonly used source code management system. GitHub is a company, website, and desktop application that makes it easier to manage Git. Microsoft purchased GitHub in 2018, so it will continue to get closer integration with Microsoft tools.
I used GitHub to store solutions to all the practical exercises that are featured at the end of each chapter. You will find the repository for this chapter at the following link: https://github.com/markjprice/cs9dotnet5.
I recommend that you add the preceding link to your favorite bookmarks because I use the GitHub repository for this book for publishing errata and other useful links.
Using Git with Visual Studio Code
Visual Studio Code has support for Git, but it will use your OS's Git installation, so you must install Git 2.0 or later first before you get these features.
You can install Git from the following link: https://git-scm.com/download.
If you...
Looking for help
This section is all about how to find quality information about programming on the web.
Reading Microsoft documentation
The definitive resource for getting help with Microsoft developer tools and platforms is Microsoft Docs, and you can find it at the following link: https://docs.microsoft.com/.
Getting help for the dotnet tool
At the command line, you can ask the dotnet
tool for help with its commands.
- To open the official documentation in a browser window for the
dotnet new
command, enter the following at the command line or in Visual Studio Code Terminal:dotnet help new
- To get help output at the command line, use the
-h
or--help
flag, as shown in the following command:dotnet new console -h
- You will see the following partial output:
Console Application (C#) Author: Microsoft Description: A project for creating a command-line application that can run on .NET Core on Windows, Linux and...
Practicing and exploring
Let's now test your knowledge and understanding by trying to answer some questions, getting some hands-on practice, and exploring with deeper research into the topics covered throughout this chapter.
Exercise 1.1 – Test your knowledge
Try to answer the following questions, remembering that although most answers can be found in this chapter, some online research or code writing will be needed to answer others:
- Why can a programmer use different languages, for example, C# and F#, to write applications that run on .NET?
- What do you type at the prompt to create a console app?
- What do you type at the prompt to build and execute C# source code?
- What is the Visual Studio Code keyboard shortcut to view Terminal?
- Is Visual Studio 2019 better than Visual Studio Code?
- Is .NET Core better than .NET Framework?
- What is .NET Standard and why is it still important?
- What is the name of the entry...
Summary
In this chapter, we:
- Set up your development environment.
- Discussed the differences between .NET 5, .NET Core, .NET Framework, Xamarin, and .NET Standard.
- Used Visual Studio Code and .NET SDK to create a simple console application.
- Learned how to download the solution code for this book from a GitHub repository.
- And most importantly, we learned how to find help.
In the next chapter, you will learn to speak C#.