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)

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/