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)

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: