Book Image

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development - Fourth Edition

By : Mark J. Price
Book Image

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development - Fourth Edition

By: Mark J. Price

Overview of this book

In C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development, Fourth Edition, expert teacher Mark J. Price gives you everything you need to start programming C# applications. This latest edition uses the popular Visual Studio Code editor to work across all major operating systems. It is fully updated and expanded with new chapters on Content Management Systems (CMS) and machine learning with ML.NET. The book covers all the topics you need. Part 1 teaches the fundamentals of C#, including object-oriented programming, and new C# 8.0 features such as nullable reference types, simplified switch pattern matching, and default interface methods. Part 2 covers the .NET Standard APIs, such as managing and querying data, monitoring and improving performance, working with the filesystem, async streams, serialization, and encryption. Part 3 provides examples of cross-platform applications you can build and deploy, such as web apps using ASP.NET Core or mobile apps using Xamarin.Forms. The book introduces three technologies for building Windows desktop applications including Windows Forms, Windows Presentation Foundation (WPF), and Universal Windows Platform (UWP) apps, as well as web applications, web services, and mobile apps.
Table of Contents (21 chapters)

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
  • 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 fixes and so on.

Using Visual Studio Code means a developer can use a cross-platform code editor to developer cross-platform apps. Therefore, I have chosen to use Visual Studio Code for all but the last two chapters for this book, because they need special features not available in Visual Studio Code for building Windows and mobile apps.

More Information: You can read about Microsoft's plans for Visual Studio Code at the following link: https://github.com/Microsoft/vscode/wiki/Roadmap.

If you prefer to use Visual Studio 2019 or Visual Studio for Mac instead of Visual Studio Code, then of course you can, but I will assume that you are already familiar with how to use them and so I will not give step-by-step instructions for using them in this book.

More Information: You can read a comparison of Visual Studio Code and Visual Studio 2019 at the following link: https://www.itworld.com/article/3403683/visual-studio-code-stepping-on-visual-studios-toes.html.

Using Visual Studio 2019 for Windows app development

Microsoft Visual Studio 2019 only runs on Windows, version 7 SP1 or later. You must run it on Windows 10 to create Universal Windows Platform (UWP) apps. It is the only Microsoft developer tool that can create Windows apps, so we will use it in Chapter 20, Building Windows Desktop Apps.

Using Visual Studio for Mac for mobile development

To create apps for the Apple operating systems like iOS to run on devices like iPhone and iPad, you must have Xcode, but that tool only runs on macOS. Although you can use Visual Studio 2019 on Windows with its Xamarin extensions to write a cross-platform mobile app, you still need macOS and Xcode to compile it.

So, we will use Visual Studio 2019 for Mac on macOS in Chapter 21, Building Cross-Platform Mobile Apps Using Xamarin.Forms.

Recommended tools for chapters

To help you to set up the best environment to use in this book, the following table summarizes which tools and operating systems I recommend be used for each of the chapters in this book:

Chapters Tool Operating systems

Chapters 1 to 19

Visual Studio Code

Windows, macOS, Linux

Chapter 20

Visual Studio 2019

Windows 10

Chapter 21

Visual Studio 2019 for Mac

macOS

To write this book, I used my MacBook Pro and the following listed software:

  • Visual Studio Code on macOS as my primary code editor.
  • Visual Studio Code on Windows 10 in a virtual machine to test OS-specific behavior like working with the filesystem.
  • Visual Studio 2019 on Windows 10 in a virtual machine to build Windows apps.
  • Visual Studio 2019 for Mac on macOS to build mobile apps.

More Information: Google and Amazon are supporters of Visual Studio Code, as you can read at the following link: https://www.cnbc.com/2018/12/20/microsoft-cmo-capossela-says-google-employees-use-visual-studio-code.html.

Deploying cross-platform

Your choice of code editor and operating system for development does not limit where your code gets deployed.

.NET Core 3.0 supports the following platforms for deployment:

  • Windows: Windows 7 SP1, or later. Windows 10 version 1607, or later. Windows Server 2012 R2 SP1, or later. Nano Server version 1803, or later.
  • Mac: macOS High Sierra (version 10.13), or later.
  • Linux: RHEL 6, or later. RHEL, CentOS, Oracle Linux version 7, or later. Ubuntu 16.04, or later. Fedora 29, or later. Debian 9, or later. openSUSE 15, or later.

More Information: You can read the official list of supported operating systems at the following link: https://github.com/dotnet/core/blob/master/release-notes/3.0/3.0-supported-os.md.

Understanding Microsoft Visual Studio Code versions

Microsoft releases a new feature version of Visual Studio Code (almost) every month and bug fix versions more frequently. For example:

  • Version 1.38, August 2019 feature release.
  • Version 1.38.1, August 2019 bug fix release.

More Information: You can read about the latest versions at the following link: https://code.visualstudio.com/updates.

The version used in this book is 1.38.1 released on 11 September 2019, but the version of Microsoft Visual Studio Code is less important than the version of the C# for Visual Studio Code extension that you will install later.

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. To support C# 8.0, you should install C# extension version 1.21.3 or later.

In this book, I will show keyboard shortcuts and screenshots of Visual Studio Code using the macOS version. Visual Studio Code on Windows and variants of Linux are practically identical, although keyboard shortcuts are likely different.

Some common keyboard shortcuts that we will use are shown in the following table:

Action macOS Windows

Show Command Palette

Cmd + Shift + P,

F1

Ctrl + Shift + P,

F1

Go To Definition

F12

F12

Go Back

Ctrl + -

Alt +

Go Forward

Ctrl + Shift + -

Alt +

Show Terminal

Ctrl + ` (backtick)

Ctrl + ' (quote)

New Terminal

Ctrl + Shift + ` (backtick)

Ctrl + Shift + ' (quote)

Toggle Line Comment

Ctrl + /

Ctrl + /

Toggle Block Comment

Shift + Option + A

Shift + Alt + A

I recommend that you download a PDF of keyboard shortcuts for your operating system from the following list:

  • Windows: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
  • macOS: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf
  • Linux: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf

More Information: You can learn about the default key bindings for Visual Studio Code and how to customize them at the following link: https://code.visualstudio.com/docs/getstarted/keybindings.

Visual Studio 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 an Insiders edition, which is a daily build of the next version.

Downloading and installing Visual Studio Code

Now you are ready to download and install Visual Studio Code, its C# extension, and the .NET Core 3.0 SDK.

  1. Download and install either the Stable build or the Insiders edition of Visual Studio Code from the following link: https://code.visualstudio.com/.
  2. Download and install the .NET Core SDK from the following link: https://www.microsoft.com/net/download.
  3. To install the C# extension, you must first launch the Visual Studio Code application.
  4. In Visual Studio Code, click the Extensions icon or navigate to ViewExtensions.
  5. C# is one of the most popular extensions available, so you should see it at the top of the list, or you can enter C# in the search box, as shown in the following screenshot:
  1. Click Install and wait for supporting packages to download and install.

More Information: You can read more about Visual Studio Code support for C# at the following link: https://code.visualstudio.com/docs/languages/csharp.

Installing other extensions

In later chapters of this book, you will use more extensions. If you want to install them now, all the extensions that we will use are shown in the following table:

Extension Description

C# for Visual Studio Code (powered by OmniSharp)

ms-vscode.csharp

C# editing support, including syntax highlighting, IntelliSense, Go to Definition, Find All References, debugging support for .NET Core (CoreCLR), and support for project.json and csproj projects on Windows, macOS, and Linux.

C# XML Documentation Comments k--kato.docomment

Generate XML documentation comments for Visual Studio Code.

C# Extensions

jchannon.csharpextensions

Add C# class, add C# interface, add fields and properties from constructors, add constructor from properties.

REST Client

humao.rest-client

Send an HTTP request and view the response directly in Visual Studio Code.

ILSpy .NET Decompiler

icsharpcode.ilspy-vscode

Decompile MSIL assemblies – support for .NET Framework, .NET Core, and .NET Standard.

SharpPad

jmazouri.sharppad

Easily inspect the results of your code. It works similarly to standalone tools like LinqPad and RoslynPad.