Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development
C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development: Build applications with C#, .NET Core, Entity Framework Core, ASP.NET Core, and ML.NET using Visual Studio Code , Fourth Edition

eBook
€8.99 €52.99
Paperback
€65.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

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

Chapter 01

Hello, C#! Welcome, .NET!

In this first chapter, the goals are setting up your development environment, understanding the similarities and differences between .NET Core, .NET Framework, and .NET Standard, and then creating the simplest application possible with C# and .NET Core 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 Core for building app features; and third, examples of common cross-platform apps you can build using C# and .NET. The last two chapters are about two types of application that can be built with C# but the first is not cross-platform and the second does not use .NET Core yet so they should be considered bonus chapters.

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 details immediately. That will be something that comes with time as you build your own apps and go beyond what any book can teach you.

In the words of Samuel Johnson, author of the English dictionary in 1755, I have committed "a few wild blunders, and risible absurdities, from which no work of such multiplicity is free." I take sole responsibility for these and hope you appreciate the challenge of my attempt to lash the wind by writing this book about rapidly evolving technologies like C# and .NET Core, and the apps that you can build with them.

This first chapter covers the following topics:

  • Setting up your development environment
  • Understanding .NET
  • Building console apps using Visual Studio Code
  • Downloading solution code from a GitHub repository
  • Looking for help

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:

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.

Understanding .NET

.NET Framework, .NET Core, Xamarin, and .NET Standard are related and overlapping platforms for developers used to build applications and services. In this section, we're going to introduce you to each of these .NET concepts.

Understanding the .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 the .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 the .NET Framework share the same version of the CLR and libraries stored in the Global Assembly Cache (GAC), which can lead to issues if some of them need a specific version for compatibility.

Good Practice: Practically speaking, .NET Framework is Windows-only and a legacy platform. Do not create new apps using it.

Understanding the Mono and Xamarin projects

Third parties developed a .NET Framework implementation named the Mono project. Mono is cross-platform, but it fell well behind the official implementation of .NET Framework.

More Information: You can read more about the project at the following link: http://www.mono-project.com/.

Mono has found a niche as the foundation of the Xamarin mobile platform as well as cross-platform game development platforms like Unity.

More Information: You can read more about Unity at the following link: https://docs.unity3d.com/.

Microsoft purchased Xamarin in 2016 and now gives away what used to be an expensive Xamarin extension for free with Visual Studio 2019. Microsoft renamed the Xamarin Studio development tool, which could only create mobile apps, to Visual Studio for Mac and gave it the ability to create other types of apps. With Visual Studio 2019 for Mac, Microsoft has replaced parts of the Xamarin Studio editor with parts from Visual Studio for Windows to provide closer parity of experience and performance.

Understanding .NET Core

Today, we live in a truly cross-platform world where modern mobile and cloud development have made Windows, as an operating system, much less important. Because of that, Microsoft has been working on an effort to decouple .NET from its close ties with Windows. While rewriting .NET Framework to be truly cross-platform, they've taken the opportunity to refactor and remove major parts that are no longer considered core.

This new product was branded .NET Core and includes a cross-platform implementation of the CLR known as CoreCLR and a streamlined library of classes known as CoreFX.

Scott Hunter, Microsoft Partner Director Program Manager for .NET, has said that "Forty percent of our .NET Core customers are brand-new developers to the platform, which is what we want with .NET Core. We want to bring new people in."

.NET Core is fast moving and because it can be deployed side by side with an app, it can change frequently knowing those changes will not affect other .NET Core apps on the same machine. Improvements that Microsoft can make to .NET Core cannot be added to .NET Framework.

More Information: You can read more about Microsoft's positioning of .NET Core and .NET Framework at the following link: https://devblogs.microsoft.com/dotnet/update-on-net-core-3-0-and-net-framework-4-8/.

Understanding future versions of .NET

At the Microsoft Build developer conference in May 2019, the .NET team announced that after .NET Core 3.0 is released in September 2019, .NET Core will be renamed .NET and the major version number will skip the number four to avoid confusion with .NET Framework 4.x. So, the next version of .NET Core will be .NET 5.0 and it is scheduled for release in November 2020. After that, Microsoft plans on annual major version releases every November, rather like Apple does major version number releases of iOS every second week in September.

More Information: You can read more about Microsoft's plans for .NET 5.0 at the following link: https://devblogs.microsoft.com/dotnet/introducing-net-5/.

The following table shows when the key versions of .NET Core were released, when future releases are planned, and which version is used by the various editions of this book:

Version Released Edition Published

.NET Core RC1

November 2015

First

March 2016

.NET Core 1.0

June 2016

.NET Core 1.1

November 2016

.NET Core 1.0.4 and .NET Core 1.1.1

March 2017

Second

March 2017

.NET Core 2.0

August 2017

.NET Core for UWP in Windows 10 Fall Creators Update

October 2017

Third

November 2017

.NET Core 2.1

May 2018

.NET Core 2.2

December 2018

.NET Core 3.0 (Current)

September 2019

Fourth

October 2019

.NET Core 3.1 (LTS)

November 2019

.NET 5.0

November 2020

.NET 6.0

November 2021

I cannot promise 5th and 6th editions of this book to match future releases of .NET, but that would be a safe bet.

Understanding .NET Core support

.NET Core versions are either Long-Term Support (LTS) or Current, as described in the following list:

  • LTS releases are stable and require fewer updates over their lifetime. These are a good choice for applications that you do not intend to update frequently. LTS releases will be supported for 3 years after general availability. .NET Core 3.1 will be an LTS release.

.NET Core 1.0 and 1.1 reached end of life and went out of support on 27 June 2019, 3 years after the initial .NET Core 1.0 release.

  • Current releases include features that may change based on feedback. These are a good choice for applications that you are actively developing because they provide access to the latest improvements. After a 3-month maintenance period, the previous minor version will no longer be supported. For example, after 1.2 releases systems running version 1.1 will have 3 months to update to 1.2 to remain eligible for support. .NET Core 3.0 is a Current release so if .NET Core 3.1 releases in November 2019 as planned, then you will need to upgrade to it by February 2020.

Both receive critical fixes throughout their lifetime for security and reliability. You must stay up to date with the latest patches to get support. For example, if a system is running 1.0 and 1.0.1 has been released, 1.0.1 will need to be installed.

What is different about .NET Core?

.NET Core is smaller than the current version of .NET Framework due to the fact that legacy technologies have been removed. For example, Windows Forms and Windows Presentation Foundation (WPF) can be used to build graphical user interface (GUI) applications, but they are tightly bound to the Windows ecosystem, so they have been removed from .NET Core on macOS and Linux.

One of the new features of .NET Core 3.0 is support for running old Windows Forms and WPF applications using the Windows Desktop Pack that is included with the Windows version of .NET Core 3.0 which is why it is bigger than the SDKs for macOS and Linux. You can make some small changes to your legacy Windows app if necessary, and then rebuild it for .NET Core to take advantage of new features and performance improvements. You'll learn about support for building these types of Windows apps in Chapter 20, Building Windows Desktop Apps.

The latest technology used to build Windows apps is the Universal Windows Platform (UWP), which is built on a custom version of .NET Core. UWP is not part of .NET Core because it is not cross-platform.

ASP.NET Web Forms and Windows Communication Foundation (WCF) are old web application and service technologies that fewer developers are choosing to use for new development projects today, so they have also been removed from .NET Core. Instead, developers prefer to use ASP.NET MVC and ASP.NET Web API. These two technologies have been refactored and combined into a new product that runs on .NET Core, named ASP.NET Core. You'll learn about the technologies in Chapter 15, Building Websites Using ASP.NET Core Razor Pages, Chapter 16, Building Websites Using the Model-View-Controller Pattern, and Chapter 18, Building and Consuming Web Services.

More Information: Some .NET Framework developers are upset that ASP.NET Web Forms, WCF, and Windows Workflow (WF) are missing from .NET Core and would like Microsoft to change their minds. There are open source projects to enable WCF and WF to migrate to .NET Core. You can read more at the following link: https://devblogs.microsoft.com/dotnet/supporting-the-community-with-wf-and-wcf-oss-projects/.

Entity Framework (EF) 6 is an object-relational mapping technology that is designed to work with data that is stored in relational databases such as Oracle and Microsoft SQL Server. It has gained baggage over the years, so the cross-platform API has been slimmed down, will be given support for non-relational databases like Microsoft Azure Cosmos DB, and named Entity Framework Core. You will learn about it in Chapter 11, Working with Databases Using Entity Framework Core.

If you have existing apps that use the old EF then version 6.3 is supported on .NET Core 3.0.

In addition to removing large pieces from .NET Framework in order to make .NET Core, Microsoft has componentized the .NET Core into NuGet packages, those being small chunks of functionality that can be deployed independently.

Microsoft's primary goal is not to make .NET Core smaller than .NET Framework. The goal is to componentize .NET Core to support modern technologies and to have fewer dependencies, so that deployment requires only those packages that your application needs.

Understanding .NET Standard

The situation with .NET in 2019 is that there are three forked .NET platforms controlled by Microsoft, as shown in the following list:

  • .NET Core: for cross-platform and new apps.
  • .NET Framework: for legacy apps.
  • Xamarin: for mobile apps.

Each has strengths and weaknesses because they are all designed for different scenarios. This has led to the problem that a developer must learn three platforms, each with annoying quirks and limitations. Because of that, Microsoft defined .NET Standard: a specification for a set of APIs that all .NET platforms can implement to indicate what level of compatibility they have. For example, basic support is indicated by a platform being compliant with .NET Standard 1.4.

With .NET Standard 2.0 and later, Microsoft made all three platforms converge on a modern minimum standard, which makes it much easier for developers to share code between any flavor of .NET.

For .NET Core 2.0 and later, this added a number of the missing APIs that developers need to port old code written for .NET Framework to the cross-platform .NET Core. However, some APIs are implemented, but throw an exception to indicate to a developer that they should not actually be used! This is usually due to differences in the operating system on which you run .NET Core. You'll learn how to handle these exceptions in Chapter 2, Speaking C#.

It is important to understand that .NET Standard is just a standard. You are not able to install .NET Standard in the same way that you cannot install HTML5. To use HTML5, you must install a web browser that implements the HTML5 standard.

To use the .NET Standard, you must install a .NET platform that implements the .NET Standard specification. .NET Standard 2.0 is implemented by the latest versions of .NET Framework, .NET Core, and Xamarin.

The latest .NET Standard, 2.1, is only implemented by .NET Core 3.0, Mono, and Xamarin. Some features of C# 8.0 require .NET Standard 2.1. .NET Standard 2.1 is not implemented by .NET Framework 4.8 so we should treat .NET Framework as legacy.

More Information: .NET Standard versions and which .NET platforms support them are listed at the following link: https://github.com/dotnet/standard/blob/master/docs/versions.md.

.NET platforms and tools used by the book editions

For the first edition of this book, which was written in March 2016, I focused on .NET Core functionality but used .NET Framework when important or useful features had not yet been implemented in .NET Core, because that was before the final release of .NET Core 1.0. Visual Studio 2015 was used for most examples, with Visual Studio Code shown only briefly.

The second edition was (almost) completely purged of all .NET Framework code examples so that readers were able to focus on .NET Core examples that truly run cross-platform. The third edition completed the switch. It was rewritten so that all of the code was pure .NET Core. But giving step-by-step instructions for multiple tools added unnecessary complexity.

In this fourth edition, we'll continue the trend by only showing coding examples using Visual Studio Code for all but the last two chapters of this book. In Chapter 20, Building Windows Desktop Apps, you'll need to use Visual Studio 2019 running on Windows 10, and in Chapter 21, Building Cross-Platform Mobile Apps Using Xamarin.Forms, you'll need to use Visual Studio 2019 for Mac.

Understanding intermediate language

The C# compiler (named Roslyn) used by the dotnet CLI tool converts your C# source code into intermediate language (IL) code and stores the IL in an assembly (a DLL or EXE file). IL code statements are like assembly language instructions, which are executed by .NET Core's virtual machine, known as CoreCLR.

At runtime, CoreCLR loads the IL code from the assembly, the just-in-time (JIT) compiler compiles it into native CPU instructions, and then it is executed by the CPU on your machine. The benefit of this three-step compilation process is that Microsoft is able to create CLRs for Linux and macOS, as well as for Windows. The same IL code runs everywhere because of the second compilation process, which generates code for the native operating system and CPU instruction set.

Regardless of which language the source code is written in, for example, C#, Visual Basic or F#, all .NET applications use IL code for their instructions stored in an assembly. Microsoft and others provide disassembler tools that can open an assembly and reveal this IL code like the ILSpy .NET Decompiler extension.

Understanding .NET Native

Another .NET initiative is called .NET Native. This compiles C# code to native CPU instructions ahead of time (AoT), rather than using the CLR to compile IL code JIT to native code later. .NET Native improves execution speed and reduces the memory footprint for applications because the native code is generated at build time and then deployed instead of the IL code.

More Information: You can read more about .NET Native at the following link: https://github.com/dotnet/corert/blob/master/Documentation/intro-to-corert.md.

Comparing .NET technologies

We can summarize and compare .NET technologies in 2019, as shown in the following table:

Technology Description Host OSes

.NET Core

Modern feature set, full C# 8.0 support, port existing and create new Windows and Web apps and services.

Windows, macOS, Linux

.NET Framework

Legacy feature set, limited C# 8.0 support, maintain existing applications.

Windows only

Xamarin

Mobile and desktop apps only.

Android, iOS, macOS

By the end of 2020, Microsoft promises that there will be a single .NET platform instead of three. .NET 5.0 is planned to have a single BCL and two runtimes: one optimized for server or desktop scenarios like websites and Windows desktop apps based on the .NET Core runtime, and one optimized for mobile apps based on the Xamarin runtime.

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 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!

  1. Start Visual Studio Code.
  2. On macOS, navigate to File | Open..., or press Cmd + O. On Windows, navigate to File | Open Folder…, or press Ctrl + K Ctrl + O. On both OSes, you can click the Open Folder button in the EXPLORER pane or click the Open Folder… link on the Welcome page, as shown in the following screenshot:
  3. In the dialog box, navigate to your user folder on macOS (mine is named markjprice), your Documents folder on Windows, or any directory or drive in which you want to save your projects.
  4. Click the New Folder button and name the folder Code.
  5. In the Code folder, create a new folder named Chapter01.
  6. In the Chapter01 folder, create a new folder named HelloCS.
  7. Select the HelloCS folder and on macOS click Open or on Windows click Select Folder.
  8. Navigate to View | Terminal, or on macOS press Ctrl + ` (backtick) and on Windows press Ctrl + ' (single quote). Confusingly on Windows the key combination Ctrl + ` (backtick) splits the current window!
  9. In TERMINAL, enter the following command:
    dotnet new console
  10. You will see that the dotnet command-line tool creates a new Console Application project for you in the current folder, and the EXPLORER window shows the two files created, HelloCS.proj and Program.cs, as shown in the following screenshot:
  11. In EXPLORER, click on the file named Program.cs to open it in the editor window. The first time that you do this, Visual Studio Code may have to download and install C# dependencies like OmniSharp, the Razor Language Server, and the .NET Core debugger, if it did not do this when you installed the C# extension.
  12. If you see a warning saying that required assets are missing, click Yes, as shown in the following screenshot:
  13. After a few seconds, a folder named .vscode will appear in the EXPLORER pane. These are used during debugging, as you will learn in Chapter 4, Writing, Debugging, and Testing Functions.
  14. In Program.cs, modify line 9 so that the text that is being written to the console says, Hello, C#!
  15. Navigate to File | Auto Save. This toggle will save the annoyance of remembering to save before rebuilding your application each time.

Compiling and running code using dotnet CLI

The next task is to compile and run the code.

  1. Navigate to View | Terminal and enter the following command:
    dotnet run
  2. The output in the TERMINAL window will show the result of running your application, as shown in the following screenshot:

Downloading solution code from a 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 recently purchased GitHub, 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/cs8dotnetcore3.

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 like to use a GUI, you can download GitHub Desktop from the following link:

https://desktop.github.com

Cloning the book solution code repository

Let's clone the book solution code repository.

  1. Create a folder named Repos in your user or Documents folder, or wherever you want to store your Git repositories.
  2. In Visual Studio Code, open the Repos folder.
  3. Navigate to View | Terminal, and enter the following command:
    git clone https://github.com/markjprice/cs8dotnetcore3.git
  1. Cloning all of the solutions for all of the chapters will take a minute or so, as shown in the following screenshot:

More Information: For more information about source code version control with Visual Studio Code, visit the following link: https://code.visualstudio.com/Docs/editor/versioncontrol.

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 used to be Microsoft Developer Network (MSDN). Now, it 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 about its commands.

  1. 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
  2. To get help output at the command line, use the -h or --help flag, as shown in the following command:
    dotnet new console -h
  3. 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 macOS
    Options:                                                     
      --langVersion  Sets langVersion in the created project file
                     text - Optional
      --no-restore   If specified, skips the automatic restore of the project on create.
                     bool - Optional
                     Default: false / (*) true
    * Indicates the value used if the switch is provided without a value.

Getting definitions of types and their members

One of the most useful keyboard shortcuts in Visual Studio Code is F12 to Go To Definition. This will show what the public definition of the type or member looks like by reading the metadata in the compiled assembly. Some tools like ILSpy .NET Decompiler will even reverse-engineer from the metadata and IL code back into C# for you.

  1. In Visual Studio Code, open the HelloCS folder.
  2. In Program.cs, inside the Main method, enter the following statement to declare an integer variable named z:
    int z;
  1. Click inside int and then press F12, or right-click and choose Go To Definition. In the new code window that appears, you can see how the int data type is defined, as shown in the following screenshot:

    You can see that int:

    • Is defined using the struct keyword.
    • Is in the System.Runtime assembly.
    • Is in the System namespace.
    • Is named Int32.
    • Is therefore an alias for the System.Int32 type.
    • Implements interfaces such as IComparable.
    • Has constant values for its maximum and minimum values.
    • Has methods like Parse.

      Good Practice: When you try to use Go To Definition you will sometimes see an error saying, No definition found. This is because the C# extension does not know about the current project. Navigate to View | Command Palette, enter and select OmniSharp: Select Project, and then select the correct project that you want to work with.

      Right now, the Go To Definition feature is not that useful to you because you do not yet know what these terms mean.

      By the end of the first part of this book, which teaches you about C#, you will know enough for this feature to become very handy.

  1. In the code editor window, scroll down to find the Parse method with a single string parameter starting on line 86, as shown in the following screenshot:

In the comment, you will see that Microsoft has documented what exceptions might occur if you call this method, including ArgumentNullException, FormatException, and OverflowException. Now, we know that we need to wrap a call to this method in a try statement and which exceptions to catch.

Hopefully, you are getting impatient to learn what all this means!

Be patient for a little longer. You are almost at the end of this chapter, and in the next chapter you will dive into the details of the C# language. But first, let's see where else you can look for help.

Looking for answers on Stack Overflow

Stack Overflow is the most popular third-party website for getting answers to difficult programming questions. It's so popular that search engines such as DuckDuckGo have a special way to write a query to search the site.

  1. Start your favorite web browser.
  1. Navigate to DuckDuckGo.com, enter the following query, and note the search results, which are also shown in the following screenshot:
    !so securestring

Searching for answers using Google

You can search Google with advanced search options to increase the likelihood of finding what you need.

  1. Navigate to Google.
  2. Search for information about garbage collection using a simple Google query, and note that you will probably see a Wikipedia definition of garbage collection in computer science, and then a list of garbage collection services in your local area, as shown in the following screenshot:
  1. Improve the search by restricting it to a useful site such as Stack Overflow, and by removing languages that we might not care about such as C++, Rust, and Python, or by adding C# and .NET explicitly, as shown in the following search query:
    garbage collection site:stackoverflow.com +C# -Java

Subscribing to the official .NET blog

To keep up to date with .NET, an excellent blog to subscribe to is the official .NET Blog written by the .NET engineering teams, and you can find it at the following link: https://blogs.msdn.microsoft.com/dotnet/

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:

  1. Why can a programmer use different languages, for example, C# and F#, to write applications that run on .NET Core?
  2. What do you type at the prompt to create a console app?
  3. What do you type at the prompt to build and execute C# source code?
  4. What is the Visual Studio Code keyboard shortcut to view Terminal?
  5. Is Visual Studio 2019 better than Visual Studio Code?
  6. Is .NET Core better than .NET Framework?
  7. What is .NET Standard and why is it important?
  8. What is the name of the entry point method of a .NET console application and how should it be declared?
  9. Where would you look for help about a C# keyword?
  10. Where would you look for solutions to common programming problems?

Exercise 1.2 – Practice C# anywhere

You don't need Visual Studio Code, or even Visual Studio 2019 or Visual Studio 2019 for Mac to write C#. You can go to .NET Fiddle - https://dotnetfiddle.net/ - and start coding online.

Microsoft is also working on an online version of Visual Studio Code that can run in any browser but currently it is only available in private preview. Eventually, it will be accessible to everyone at the following link: https://online.visualstudio.com/

Exercise 1.3 – Explore topics

You can use the following links to read more details about the topics we've covered in this chapter:

Summary

In this chapter, we set up your development environment, discussed the differences between .NET Framework, .NET Core, Xamarin, and .NET Standard, we used Visual Studio Code and .NET Core SDK to create a simple console application, we learned how to download the solution code for this book from a GitHub repository, and most importantly, how to find help.

In the next chapter, you will learn to speak C#.

Left arrow icon Right arrow icon

Key benefits

  • Build modern, cross-platform applications with .NET Core 3.0
  • Get up to speed with C#, and up to date with all the latest features of C# 8.0
  • Start creating professional web applications with ASP.NET Core 3.0

Description

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.

Who is this book for?

Readers with some prior programming experience or with a science, technology, engineering, or mathematics (STEM) background, who want to gain a solid foundation with C# 8.0 and .NET Core 3.0.

What you will learn

  • Build cross-platform applications for Windows, macOS, Linux, iOS, and Android
  • Explore application development with C# 8.0 and .NET Core 3.0
  • Explore ASP.NET Core 3.0 and create professional web applications
  • Learn object-oriented programming and C# multitasking
  • Query and manipulate data using LINQ
  • Use Entity Framework Core and work with relational databases
  • Discover Windows app development using the Universal Windows Platform and XAML
  • Build mobile applications for iOS and Android using Xamarin.Forms

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 31, 2019
Length: 818 pages
Edition : 4th
Language : English
ISBN-13 : 9781788471572
Vendor :
Microsoft
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Oct 31, 2019
Length: 818 pages
Edition : 4th
Language : English
ISBN-13 : 9781788471572
Vendor :
Microsoft
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 128.97
C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development
€65.99
Hands-On Design Patterns with C# and .NET Core
€29.99
Hands-On Network Programming with C# and .NET Core
€32.99
Total 128.97 Stars icon
Banner background image

Table of Contents

21 Chapters
Hello, C#! Welcome, .NET! Chevron down icon Chevron up icon
Speaking C# Chevron down icon Chevron up icon
Controlling Flow and Converting Types Chevron down icon Chevron up icon
Writing, Debugging, and Testing Functions Chevron down icon Chevron up icon
Building Your Own Types with Object-Oriented Programming Chevron down icon Chevron up icon
Implementing Interfaces and Inheriting Classes Chevron down icon Chevron up icon
Understanding and Packaging .NET Types Chevron down icon Chevron up icon
Working with Common .NET Types Chevron down icon Chevron up icon
Working with Files, Streams, and Serialization Chevron down icon Chevron up icon
Protecting Your Data and Applications Chevron down icon Chevron up icon
Working with Databases Using Entity Framework Core Chevron down icon Chevron up icon
Querying and Manipulating Data Using LINQ Chevron down icon Chevron up icon
Improving Performance and Scalability Using Multitasking Chevron down icon Chevron up icon
Practical Applications of C# and .NET Chevron down icon Chevron up icon
Building Websites Using ASP.NET Core Razor Pages Chevron down icon Chevron up icon
Building Websites Using the Model-View-Controller Pattern Chevron down icon Chevron up icon
Building Websites Using a Content Management System Chevron down icon Chevron up icon
Building and Consuming Web Services Chevron down icon Chevron up icon
Building Intelligent Apps Using Machine Learning Chevron down icon Chevron up icon
Building Windows Desktop Apps Chevron down icon Chevron up icon
Building Cross-Platform Mobile Apps Using Xamarin.Forms Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(71 Ratings)
5 star 50.7%
4 star 15.5%
3 star 8.5%
2 star 9.9%
1 star 15.5%
Filter icon Filter
Top Reviews

Filter reviews by




Richie May 10, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Absolutely brilliant, i have been learning C# and have done some video courses and then brought this book. The book goes into detail whichbis brilliant for me. It explains things very well and gives you an idea of when theybwere implemented which add to the experience so you identify how new a feature is.Online courses are good but this book showed me loads of new things that wernt taught the author gives his opinion on somethings also if some techniques are not used often or if theybare frowned upon.It has a good practices section under each topic which tells younwhat you should do.I really like this book it has the detail you would want while teaching the content quickly.It gives reference links for further reading whichbos handy.
Amazon Verified review Amazon
Scott Crowder Sep 14, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I'm an very experienced C# and .NET engineer and picked up this book to get up to speed on the latest updates to the language and the platform. I found the book easy to navigate. The first few chapters cover the basics. The later chapters do a great job of covering the C# 8 and latest .NET Core features. Mark does an excellent job showing the new features and the why & how to you want to use the latest stuff.
Amazon Verified review Amazon
Christof Stanits Dec 23, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am currently halfway through the book (it is a lot!)No nonsense, straight to topic textbook.It is packed with information and gives you an excellent guideline to learn all you need to know about C# 8.0 and .NET Core 3.0 and how to use it to develop apps.The title is very very long but its really all in there.You might need a very basic understanding of "C#/programming in general" to not feel overwhelmed by its pace, BUT Everything you need to know IS covered.
Amazon Verified review Amazon
Alexandre Malavasi Nov 29, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I just took a look at the preview version of the book C# 8.0 and .NET Core 3.0 — Modern Cross-Platform Development and I found it really impressive, covering almost everything in .NET Core 3.0 and focused on what is really important to know. Written by Mark J. Price, the entire book, with more than 800 pages, contains basic and advanced content, covering all the existent features on .NET Core.I did like the way that the author explored the history of .NET, explaining the differences between .NET Framework and .NET Core. Also, it has tons of examples on how we can migrate our projects to the newest version of .NET Core. That represents a good challenge, mainly in big and complex projects, and the book is really useful to understand the pros and cons of keeping our projects in .NET Framework or migrating it to a newer version.For creating powerful applications using .NET Core, with high performance and world-class quality, it is mandatory to understand entirely what .NET Core offers to you and to understand with details how you can integrate different resources and features of .NET Core in multiples solutions. I think the book meets its objective perfectly.I have really enjoyed the book.I would like to highly recommend this book to everyone who wants to learn C# .NET Core. Very well-drafted book with lots of insights and definitely the best one.
Amazon Verified review Amazon
N Bishai Dec 01, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent and fast delivery
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.