Hello, C#! Welcome, .NET!
In this first chapter, the goals are setting up your development environment, understanding the similarities and differences between modern .NET, .NET Core, .NET Framework, Mono, Xamarin, and .NET Standard, creating the simplest application possible with C# 10 and .NET 6 using various code editors, and then discovering good places to look for help.
The GitHub repository for this book has solutions using full application projects for all code tasks and notebooks when possible:
Simply press the . (dot) key or change
.dev in the link above to change the GitHub repository into a live editor using Visual Studio Code for the Web, as shown in Figure 1.1:
Figure 1.1: Visual Studio Code for the Web live editing the book's GitHub repository
This is great to run alongside your chosen code editor as you work through the book's coding tasks. You can compare your code to the solution code...
Setting up your development environment
- Visual Studio 2022 for Windows
- Visual Studio 2022 for Mac
- Visual Studio Code for Windows, Mac, or Linux
- GitHub Codespaces
Third parties have created their own C# code editors, for example, JetBrains Rider.
Choosing the appropriate tool and application type for learning
What is the best tool and application type for learning C# and .NET?
When learning, the best tool is one that helps you write code and configuration but does not hide what is really happening. IDEs provide graphical user interfaces that are friendly to use, but what are they doing for you underneath? A more basic code editor that is closer to the action while providing help to write your code is better while you are learning.
Having said that, you can make...
.NET 6, .NET Core, .NET Framework, 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.
Since .NET Framework 4.5.2, it has been an official component of the Windows operating system. Components have the same support as their parent products, so 4.5.2 and later follow the life cycle policy of the Windows OS on which it is installed. .NET Framework is installed on over one billion computers...
Building console apps using Visual Studio 2022
If you do not have a Windows computer or you want to use Visual Studio Code, then you can skip this section since the code will be the same, just the tooling experience is different.
Managing multiple projects using Visual Studio 2022
Visual Studio 2022 has a concept named a solution that allows you to open and manage multiple projects simultaneously. We will use a solution to manage the two projects that you will create in this chapter.
Writing code using Visual Studio 2022
- Start Visual Studio 2022.
- In the Start window, click Create a new project.
- In the Create a new project dialog, enter
consolein the Search for templates box, and select Console Application, making sure that you have chosen the C# project template rather than another language, such as...
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.
If you never want to try Visual Studio Code or .NET Interactive Notebooks, then please feel free to skip this section and the next, and then continue with the Reviewing the folders and files for projects section.
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.
Managing multiple projects using Visual Studio Code
Exploring code using .NET Interactive Notebooks
Creating a notebook
- In Visual Studio Code, close any open workspaces or folders.
- Navigate to View | Command Palette.
.net inter, and then select .NET Interactive: Create new blank notebook, as shown in Figure 1.13:
Figure 1.13: Creating a new blank .NET notebook
- When prompted to select the file extension, choose Create as '.dib'.
.dibis an experimental file format defined by Microsoft to avoid confusion and compatibility issues with the .
ipynbformat used by Python interactive notebooks. The file extension was historically only for Jupyter notebooks that can contain an interactive (I) mix of data, Python code (PY), and output in a notebook...
Reviewing the folders and files for projects
In this chapter, you created two projects named
The result is a folder structure and files that will be repeated in subsequent chapters, although with more than just two projects, as shown in Figure 1.18:
Figure 1.18: Folder structure and files for the two projects in this chapter
Understanding the common folders and files
.sln files are different, the project folders and files such as
TopLevelProgram are identical for Visual Studio 2022 and Visual Studio Code. This means that you can mix and match between both code editors if you like:
- In Visual Studio 2022, with a solution open, navigate to File | Add Existing Project… to add a project file...
Making good use of the GitHub repository for this book
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.
- To store the solution code for the book that can be maintained after the print publication date.
- To provide extra materials that extend the book, like errata fixes, small improvements, lists of useful links, and longer articles that cannot fit in the printed book.
- To provide a place for readers to get in touch with me if they have issues with the book.
Raising issues with the book
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
- To open the official documentation in a browser window for the
dotnet newcommand, enter the following at the command line or in the Visual Studio Code terminal:
dotnet help new
- To get help output at the command line, use the
--helpflag, 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...
Practicing and exploring
Let's now test your knowledge and understanding by trying to answer some questions, getting some hands-on practice, and going into the topics covered throughout this chapter in greater detail.
Exercise 1.1 – Test your knowledge
Try to answer the following questions, remembering that although most answers can be found in this chapter, you should do some online research or code writing to answer others:
- Is Visual Studio 2022 better than Visual Studio Code?
- Is .NET 6 better than .NET Framework?
- What is .NET Standard and why is it still important?
- Why can a programmer use different languages, for example, C# and F#, to write applications that run on .NET?
- What is the name of the entry point method of a .NET console application and how should it be declared?
- What is a top-level program and how do you access any command-line arguments?
- What do you type at the prompt to build and execute C# source...
In this chapter, we:
- Set up your development environment.
- Discussed the similarities and differences between modern .NET, .NET Core, .NET Framework, Xamarin, and .NET Standard.
- Used Visual Studio Code with the .NET SDK and Visual Studio 2022 for Windows to create some simple console applications.
- Used .NET Interactive Notebooks to execute snippets of code for learning.
- Learned how to download the solution code for this book from a GitHub repository.
- And, most importantly, learned how to find help.
In the next chapter, you will learn how to "speak" C#.