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: Visual Studio 2022 for Windows. (Visual Studio 2022 for Mac reaches end-of-life on August 31, 2024, and is not recommended.)
- Code: Visual Studio Code for Windows, Mac, Linux, the Web, or GitHub Codespaces.
- Rider: JetBrains Rider, which is available for Windows, Mac, or Linux but does have a license cost. Rider is popular with more experienced .NET developers.
Choosing the appropriate tool and application type for learning
What is the best tool and application type for learning how to use tools and skills with C# and .NET?
I want you to be free to choose any C# code editor or IDE to complete the coding tasks in this book, including Visual Studio, Code, Rider, or a code editor that I’ve never heard of.
In this book, I give general instructions that work with all tools so you can use whichever tool you prefer.
Using Visual Studio for general development
Visual Studio can create most types of applications, including console apps, websites, web services, desktop, and mobile apps. Visual Studio only runs on Windows 10 version 1909 or later, or Windows Server 2016 or later, and only on 64-bit versions. Version 17.4 is the first version to support native ARM64.
Warning!
Visual Studio for Mac does not officially support .NET 8 and it will reach end-of-life in August 2024. If you have been using Visual Studio for Mac then you should switch to Code for Mac or Rider for Mac, or use Visual Studio in a virtual machine on your local computer or in the cloud using a technology like Microsoft Dev Box. The retirement announcement can be read here: https://devblogs.microsoft.com/visualstudio/visual-studio-for-mac-retirement-announcement/.
Using Code for cross-platform development
The most modern and lightweight code editor to choose from, and the only one from Microsoft that is cross-platform, is Code. It can run on all common operating systems, including Windows, macOS, and many varieties of Linux, including Red Hat Enterprise Linux (RHEL) and Ubuntu. 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#.
Being cross-platform and lightweight, it can be installed on all platforms that your apps will be deployed to for quick bug fixes and so on. Choosing Code means a developer can use a cross-platform code editor to develop cross-platform apps.
Code is by far the most popular IDE, with over 74% of professional developers selecting it in the Stack Overflow 2023 survey, which you can read at the following link: https://survey.stackoverflow.co/2023/#most-popular-technologies-new-collab-tools-prof.
Using GitHub Codespaces for development in the cloud
GitHub Codespaces is a fully configured development environment based on Code that can be spun up in an environment hosted in the cloud and accessed through any web browser. It supports Git repositories, extensions, and a built-in command-line interface so you can edit, run, and test from any device. Since it runs in the cloud and you interact with it via a web browser there is nothing to download and install, and since it is based on Code, it works the same way Code does on your local computer.
Reviews of GitHub Codespaces are available at the following links:
Using Rider for cross-platform development
Rider is a third-party code editor from JetBrains, the maker of IntelliJ IDEA, the leading Java and Kotlin IDE, and ReSharper, the popular Visual Studio plugin for .NET developers. Rider runs on multiple platforms: Windows, macOS, and Linux.
It has a license fee, but developers who use it are often professionals who appreciate the extra features it has like live code inspections, context actions, and refactorings. Using Rider can be an indicator of a more advanced developer.
You should expect new versions of Rider to support the latest version of .NET a few weeks after Visual Studio. For example, .NET 8 was released on November 14, 2023, and Visual Studio with official support for .NET 8 was released on the same day. But Rider version 2023.3 with official support for .NET 8 was not released until December 7, 2023.
Although JetBrains is a fantastic company with great products, both Rider and the ReSharper extension for Visual Studio are software, and all software has bugs and quirky behavior. For example, they might show errors like “Cannot resolve symbol” in your Razor Pages, Razor views, and Blazor components. Yet, you can build and run those files because there is no actual problem.
If you use Rider and you have installed the Unity Support or Heap Allocation Viewer plugins, then it will complain a lot about boxing. A common scenario when boxing happens is when value types like int
and DateTime
are passed as positional arguments to string
formats. This is a problem for Unity projects because they use a different memory garbage collector than the normal .NET runtime. For non-Unity projects, like all the projects in this book, you can ignore these boxing warnings because they are not relevant. You can read more about this Unity-specific issue at the following link: https://docs.unity3d.com/Manual/performance-garbage-collection-best-practices.html#boxing.
What I used
To write and test the code for this book, I used the following hardware and software:
- Visual Studio on:
- Windows 11 on an HP Spectre (Intel) laptop
- Code on:
- macOS on an Apple Silicon Mac mini (M1) desktop
- Windows 11 on an HP Spectre (Intel) laptop
- Rider on:
- Windows 11 on an HP Spectre (Intel) laptop
- macOS on an Apple Silicon Mac mini (M1) desktop
I hope that you have access to a variety of hardware and software too because seeing the differences on various platforms deepens your understanding of development challenges, although any one of the preceding combinations is enough to learn how to build practical apps and websites.
Deploying cross-platform
Your choice of code editor and operating system for development does not limit where your code gets deployed.
.NET 8 supports the following platforms for deployment:
- Windows: Windows 10 version 1607 or later. Windows 11 version 22000 or later. Windows Server 2012 R2 SP1 or later. Nano Server version 1809 or later.
- Mac: macOS Catalina version 10.15 or later and in the Rosetta 2 x64 emulator. Mac Catalyst: 11.0 or later.
- Linux: Alpine Linux 3.17 or later. Debian 11 or later. Fedora 37 or later. openSUSE 15 or later. Oracle Linux 8 or later. Red Hat Enterprise Linux (RHEL) 8 or later. SUSE Enterprise Linux 12 SP2 or later. Ubuntu 20.04 or later.
- Android: API 21 or later.
- iOS and tvOS: 11.0 or later.
Downloading and installing Visual Studio
Many professional .NET developers use Visual Studio in their day-to-day development work. Even if you choose to use Code to complete the coding tasks in this book, you might want to familiarize yourself with Visual Studio too.
If you do not have a Windows computer, then you can skip this section and continue to the next section, where you will download and install Code on macOS or Linux.
Since October 2014, Microsoft has made a professional quality edition of Visual Studio available to students, open-source contributors, and individuals for free. It is called Community Edition. Any of the editions are suitable for this book. If you have not already installed it, let’s do so now:
- Download Visual Studio version 17.10 or later from the following link: https://visualstudio.microsoft.com/downloads/.
You must install version 17.10 or later for the new Copilot experience.
- Start the installer.
- On the Workloads tab, select the following:
- ASP.NET and web development
- .NET desktop development (because this includes console apps).
- Desktop development with C++ with all default components (because this enables publishing console apps and web services that start faster and have smaller memory footprints).
- On the Individual components tab, in the Code tools section, select the following:
- GitHub Copilot
- Git for Windows
- Click Install and wait for the installer to acquire the selected software and install it.
- When the installation is complete, click Launch.
- The first time that you run Visual Studio, you will be prompted to sign in. If you have a Microsoft account, you can use that account. If you don’t, then register for a new one at the following link: https://signup.live.com/.
- The first time that you run Visual Studio, you will be prompted to configure your environment. For Development Settings, choose Visual C#. For the color theme, I chose Blue, but you can choose whatever tickles your fancy.
- If you want to customize your keyboard shortcuts, navigate to Tools | Options…, and then select the Environment | Keyboard option.
Visual Studio keyboard shortcuts
In this book, I will avoid showing keyboard shortcuts since they are often customized. Where they are consistent across code editors and commonly used, I will try to show them.
If you want to identify and customize your keyboard shortcuts, then you can, as shown at the following link: https://learn.microsoft.com/en-us/visualstudio/ide/identifying-and-customizing-keyboard-shortcuts-in-visual-studio.
Visual Studio Enterprise edition tools
Visual Studio Enterprise edition is designed for larger teams and organizations with advanced development, testing, and deployment needs. It adds exclusive capabilities tailored for enterprise-scale projects and advanced development scenarios. Licenses for the Enterprise edition cost thousands of dollars so are typically limited to people who work for large organizations. Therefore, I do not cover these features and tools in this book.
Some tools that are exclusive to Visual Studio Enterprise edition are shown in the following list:
- IntelliTrace allows developers to record and replay application execution, enhancing the debugging process by providing a historical context of what happened before an issue occurred.
- Code Map visualizes and explores complex codebases, making it easier to understand and navigate relationships and dependencies within the code.
- Live Unit Testing automatically runs impacted unit tests in the background and displays real-time results and code coverage, ensuring immediate feedback on changes.
- Microsoft Fakes supports the creation of shim and stub objects, enabling the testing of code in isolation by replacing parts of the application with mock objects.
- Web load and performance testing allow developers to create, manage, and execute performance tests to evaluate the scalability and responsiveness of web applications under heavy load.
- Coded UI testing supports the creation of automated tests for the user interface of applications, ensuring that the UI behaves as expected.
- CodeLens enhances code understanding by providing insights directly within the editor, such as change history, code references, and work item details, without the need to navigate away from the code.
- Architecture and dependency validation help ensure that development efforts adhere to predefined architecture dependencies and layering standards and constraints, preventing architectural drift.
- Advanced static code analysis offers more comprehensive rules and capabilities for analyzing code quality and potential issues, beyond what is available in the other editions.
- Release management provides advanced release management features and tools for automating the deployment process, helping teams deliver software more quickly and reliably.
- Snapshot debugger allows developers to take snapshots of their production environments, enabling them to inspect the state and debug issues without impacting the live environment.
- App Center Test allows automated UI testing on thousands of real devices in the cloud.
Downloading and installing Code
Code has rapidly improved over the past couple of years and has pleasantly surprised Microsoft with its popularity. If you are brave and like to live on the bleeding edge, then there is the Insiders edition, which is a daily build of the next version.
Even if you plan to only use Visual Studio for development, I recommend that you download and install Code and try the coding tasks in this chapter using it, and then decide if you want to stick with just using Visual Studio for the rest of the book.
Let’s now download and install Code, the .NET SDK, and the C# Dev Kit extension:
- Download and install either the Stable Build or the Insiders edition of Code from the following link: https://code.visualstudio.com/.
- Download and install the .NET SDK for version 8.0 from the following link: https://www.microsoft.com/net/download.
Good Practice
The latest SDK version at the time of writing in June 2024 is 8.0.301. Always install or update to the latest patched version.
- To install the C# Dev Kit extension using the user interface, you must first launch the Code application.
- In Code, click the Extensions icon or navigate to View | Extensions.
- C# Dev Kit is one of the most popular extensions available, so you should see it at the top of the list, or you can enter
C# Dev Kit
in the search box.
C# Dev Kit has a dependency on the C# extension version 2.0 or later, so you do not have to install the C# extension separately. Note that C# extension version 2.0 or later no longer uses OmniSharp since it has a new Language Service Protocol (LSP) host. C# Dev Kit also has dependencies on the .NET Install Tool for Extension Authors and IntelliCode for C# Dev Kit extensions, so they will be installed, too.
- Click Install and wait for supporting packages to download and install.
Installing other extensions
In later chapters of this book, you will use more Code extensions. If you want to install them now, all the extensions that we will use are shown in Table 1.1:
Extension name and identifier
|
Description
|
C# Dev Kit
ms-dotnettools.csdevkit
|
An official C# extension from Microsoft. Manage your code with a solution explorer and test your code with integrated unit test discovery and execution.
It includes the C# and IntelliCode for C# Dev Kit extensions.
|
C#
ms-dotnettools.csharp
|
This offers C# editing support, including syntax highlighting, IntelliSense, Go To Definition, Find All References, debugging support for .NET, and support for csproj projects on Windows, macOS, and Linux.
|
IntelliCode for C# Dev Kit
ms-dotnettools.vscodeintellicode-csharp
|
This provides AI-assisted development features for Python, TypeScript/JavaScript, C#, and Java developers.
|
MSBuild project tools
tintoy.msbuild-project-tools
|
This provides IntelliSense for MSBuild project files, including autocomplete for <PackageReference> elements.
|
SQL Server (mssql) for Visual Studio Code
ms-mssql.mssql
|
This is for developing SQL Server, Azure SQL Database, and SQL Data Warehouse everywhere with a rich set of functionalities.
|
REST Client
humao.rest-client
|
With this, you can send an HTTP request and view the response directly in Code.
|
ilspy-vscode
icsharpcode.ilspy-vscode
|
With this, you can decompile Microsoft Intermediate Language (MSIL) aka .NET assemblies. It supports modern .NET, .NET Framework, .NET Core, and .NET Standard.
|
Table 1.1: Code extensions used in this book
Managing Code extensions at the command prompt
You can manage Code extensions at the command prompt or terminal, as shown in Table 1.2:
Command
|
Description
|
code --list-extensions
|
List installed extensions.
|
code --install-extension <extension-id>
|
Install the specified extension.
|
code --uninstall-extension <extension-id>
|
Uninstall the specified extension.
|
Table 1.2: Working with extensions at the command prompt
For example, to install the C# Dev Kit extension, enter the following at the command prompt:
code --install-extension ms-dotnettools.csdevkit
Understanding Code versions
Microsoft releases a new feature version of Code (almost) every month and bug-fix versions more frequently. For example:
- Version 1.90.0, May 2024 feature release
- Version 1.90.1, May 2024 bug fix release
The version of Code is less important than the version of the C# extension. While the C# extension is not required, it provides IntelliSense as you type, code navigation, and debugging features, so it’s something that’s very handy to install and keep updated to support the latest C# language features.
Code keyboard shortcuts
In this book, I will avoid showing keyboard shortcuts used for tasks like creating a new file since they are often different on different operating systems. The situations where I will show keyboard shortcuts are when you need to repeatedly press the key, for example, while debugging. These are also more likely to be consistent across operating systems.
If you want to customize your keyboard shortcuts for Code, then you can, as shown at the following link: https://code.visualstudio.com/docs/getstarted/keybindings.
I recommend that you download a PDF of keyboard shortcuts for your operating system from the following list:
Downloading and installing Rider
According to the Stack Overflow Survey 2023, about 20% of professional C# and .NET developers use Rider in their day-to-day development work. Rider is cross-platform so you can install and run it on Windows, macOS, and varieties of Linux. You can use an evaluation license key for a free 30-day trial.
JetBrains also offers Toolbox App to manage your IDEs easily and it’s free. It is especially useful for Linux users because Rider does not update through the apt
(Advanced Package Tool) command like normal Linux programs. It also gives you access to early access programs for software like JetBrains Fleet, a next-generation code editor.
If you have not already installed Rider and you want to use it, let’s do so now:
- Download the latest version of Rider from the following link: https://www.jetbrains.com/rider/download/.
If you’re on Ubuntu 16.04 or later, you can install Rider from the command prompt or terminal, as shown in the following command: sudo snap install rider --classic
- Run the installer.
- Download the latest version of JetBrains Toolbox App from the following link: https://www.jetbrains.com/toolbox-app/.
- Run the installer.
- Navigate to https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2022 and install the Build Tools for Visual Studio. These are necessary to use the native ahead-of-time (AOT) compiler.
If you have installed JetBrains ReSharper in Visual Studio, you will get the same tools as in Rider.
Other JetBrains tools
JetBrains offers a suite of tools designed to enhance productivity, code quality, and application performance for .NET developers. A brief overview of dotPeek, dotTrace, dotMemory, and dotCover, highlighting their capabilities and benefits, is included in the following list:
- dotPeek: This tool can decompile .NET assemblies back to readable C# code, making it easier to explore and understand external code that lacks a source. It allows browsing the contents of assemblies, helping developers explore the structure and dependencies without needing the source code. It provides advanced navigation and search capabilities within decompiled code, such as quick jumping to symbol definitions and usages. It’s invaluable for understanding the inner workings of third-party libraries, frameworks, or any compiled .NET code without the original source. It can serve as a symbol server, making it possible to step into decompiled code during debugging sessions, aiding in the diagnosis of issues with external libraries. dotPeek is offered as a free tool, making advanced decompilation and assembly exploration accessible to all .NET developers.
- dotTrace: This tool provides detailed performance profiling for .NET applications, identifying performance bottlenecks by measuring execution times and call frequencies. It supports sampling, tracing, and timeline modes, each suited to different types of performance analysis. It offers a comprehensive set of views and filters to analyze the profiling data, including call trees, hot spots, and timeline views. It helps developers optimize application performance by pinpointing slow-running code and inefficient algorithms.
- dotMemory: This tool analyzes memory usage of .NET applications, identifying memory leaks and issues related to Garbage Collector (GC). It helps understand how memory is allocated and released, pinpointing excessive allocations that could lead to performance degradation. It allows the comparison of memory snapshots taken at different times, making it easier to identify memory leaks and the objects responsible. It facilitates memory optimization by providing insights into memory consumption patterns and detecting memory leaks.
- dotCover: This tool measures how much of the codebase is covered by tests, highlighting covered and uncovered code blocks. It works with various testing frameworks and supports all .NET applications, enhancing test effectiveness. It provides a continuous testing mode that automatically runs tests in the background as code changes, offering immediate feedback on test coverage and success. It helps improve code quality by ensuring higher coverage of tests, which can lead to fewer bugs and more stable applications. It facilitates an efficient testing process by identifying untested code, allowing developers to focus on areas lacking test coverage.
Together, these JetBrains tools form a comprehensive toolkit that can significantly enhance the productivity and efficiency of .NET development teams.
Chrome AI tools
Chrome now has AI tools built-in that you can read about at the following link: https://developer.chrome.com/docs/devtools/console/understand-messages.