Book Image

C# 7.1 and .NET Core 2.0 ??? Modern Cross-Platform Development - Third Edition

By : Mark J. Price
Book Image

C# 7.1 and .NET Core 2.0 ??? Modern Cross-Platform Development - Third Edition

By: Mark J. Price

Overview of this book

C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development, Third Edition, is a practical guide to creating powerful cross-platform applications with C# 7.1 and .NET Core 2.0. It gives readers of any experience level a solid foundation in C# and .NET. The first part of the book runs you through the basics of C#, as well as debugging functions and object-oriented programming, before taking a quick tour through the latest features of C# 7.1 such as default literals, tuples, inferred tuple names, pattern matching, out variables, and more. After quickly taking you through C# and how .NET works, this book dives into the .NET Standard 2.0 class libraries, covering topics such as packaging and deploying your own libraries, and using common libraries for working with collections, performance, monitoring, serialization, files, databases, and encryption. The final section of the book demonstrates the major types of application that you can build and deploy cross-device and cross-platform. In this section, you'll learn about websites, web applications, web services, Universal Windows Platform (UWP) apps, and mobile apps. By the end of the book, you'll be armed with all the knowledge you need to build modern, cross-platform applications using C# and .NET.
Table of Contents (31 chapters)
Title Page
Credits
About the Author
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
2
Part 1 – C# 7.1
8
Part 2 – .NET Core 2.0 and .NET Standard 2.0
16
Part 3 – App Models
22
Summary
Index

Preface

There are C# books that are thousands of pages long that aim to be comprehensive references to the C# programming language and the .NET Framework.

This book is different. It is concise and aims to be a fast-paced read that is packed with hands-on walkthroughs. I wrote this book to be the best step-by-step guide to learning modern cross-platform C# proven practices using .NET Core.

I will point out the cool corners and gotchas of C#, so you can impress colleagues and employers and get productive fast. Rather than slowing down and boring some readers by explaining every little thing, I will assume that if a term I use is new to you, then you will know how to Google an answer.

At the end of each chapter is a section titled Practice and explore, in which you will complete hands-on practical exercises and explore topics deeper on your own with a little nudge in the right direction from me.

You can download solutions for the exercises from the following GitHub repository. I will provide instructions on how to do this using Visual Studio 2017 and Visual Studio Code at the end of Chapter 1, Hello, C#! Welcome, .NET Core!

https://github.com/markjprice/cs7dotnetcore2

What this book covers

Chapter 1, Hello, C#! Welcome, .NET Core!, is about setting up your development environment and using various tools to create the simplest application possible with C#. You will learn how to write and compile code using Visual Studio 2017 on Windows; Visual Studio Code on macOS, Linux, or Windows; or Visual Studio for Mac on macOS. You will learn the different .NET technologies: .NET Framework, .NET Core, .NET Standard, and .NET Native.

Part 1 – C# 7.1

Chapter 2, Speaking C#, explains the grammar and vocabulary that you will use every day to write the source code for your applications. In particular, you will learn how to declare and work with variables of different types.

Chapter 3, Controlling the Flow and Converting Types, talks about writing code that makes decisions, repeats a block of statements, and converts between types, and writing code defensively to handle errors when they inevitably occur. You will also learn the best places to look for help.

Chapter 4, Writing, Debugging, and Testing Functions, is about following the Don't Repeat Yourself (DRY) principle by writing reusable functions, and learning how to use debugging tools to track down and remove bugs, monitoring your code while it executes to diagnose problems, and rigorously testing your code to remove bugs and ensure stability and reliability before it gets deployed into production.

Chapter 5, Building Your Own Types with Object-Oriented Programming, discusses all the different categories of members that a type can have, including fields to store data and methods to perform actions. You will use OOP concepts, such as aggregation and encapsulation. You will learn the C# 7 language features such as tuple syntax support and out variables, and C# 7.1 language features such as default literals and inferred tuple names.

Chapter 6, Implementing Interfaces and Inheriting Classes, explains deriving new types from existing ones using object-oriented programming (OOP). You will learn how to define operators and C# 7 local functions, delegates and events, how to implement interfaces about base and derived classes, how to override a type member, how to use polymorphism, how to create extension methods, and how to cast between classes in an inheritance hierarchy.

Part 2 – .NET Core 2.0 and .NET Standard 2.0

Chapter 7, Understanding and Packaging .NET Standard Types, presents .NET Core 2.0 types that are part of .NET Standard 2.0, and how they are related to C#.  You will learn how to deploy and package your own apps and libraries.

Chapter 8, Using Common .NET Standard Types, discusses the .NET Standard types that allow your code to perform common practical tasks, such as manipulating numbers and text, storing items in collections, and implementing internationalization.

Chapter 9, Working with Files, Streams, and Serialization, talks about interacting with the filesystem, reading and writing to files and streams, text encoding, and serialization.

Chapter 10, Protecting Your Data and Applications, is about protecting your data from being viewed by malicious users using encryption and from being manipulated or corrupted using hashing and signing. You will also learn about authentication and authorization to protect applications from unauthorized uses.

Chapter 11, Working with Databases Using Entity Framework Core, explains reading and writing to databases, such as Microsoft SQL Server and SQLite, using the object-relational mapping technology named Entity Framework Core.

Chapter 12, Querying and Manipulating Data Using LINQ, teaches you Language INtegrated Query (LINQ)—language extensions that add the ability to work with sequences of items and filter, sort, and project them into different outputs.

Chapter 13, Improving Performance and Scalability Using Multitasking, discusses allowing multiple actions to occur at the same time to improve performance, scalability, and user productivity. You will learn about the C# 7.1 async Main feature, and how to use types in the System.Diagnostics namespace to monitor your code to measure performance and efficiency.

Part 3 – App Models

Chapter 14, Building Web Sites Using ASP.NET Core Razor Pages, is about learning the basics of building websites with a modern HTTP architecture on the server-side using ASP.NET Core. You will learn the new ASP.NET Core feature known as Razor Pages that simplifies creating web pages for small web sites.

Chapter 15, Building Web Sites Using ASP.NET Core MVC, is about learning how to build large, complex websites in a way that is easy to unit test and manage with teams of programmers using ASP.NET Core. You will learn about startup configuration, authentication, routes, models, views, and controllers in ASP.NET Core MVC.

Chapter 16, Building Web Services and Applications Using ASP.NET Core, explains building web applications with a combination of a modern frontend technology, such as Angular or React, and a backend REST architecture web service using ASP.NET Core Web API.

Chapter 17, Building Windows Apps Using XAML and Fluent Design, talks about learning the basics of XAML that can be used to define the user interface for a graphical app for the Universal Windows Platform (UWP), and applying principles and features of Fluent Design to light it up. This app can then run on any device running Windows 10, Xbox One, and even Mixed Reality devices such as HoloLens.

Chapter 18, Building Mobile Apps Using XAML and Xamarin.Forms, discusses introducing you to taking C# mobile by building a cross-platform app for iOS and Android. The client-side mobile app will be created with Visual Studio for Mac using XAML and Xamarin.Forms.

Appendix, Answers to the Test Your Knowledge Questions, has the answers to the test questions at the end of each chapter.

What you need for this book

You can develop and deploy C# on many platforms, including Windows, macOS, and many varieties of Linux. For the best programming experience, and to reach the most platforms, I recommend that you learn the basics of all members of the Visual Studio family: Visual Studio 2017, Visual Studio Code, and Visual Studio for Mac.

My recommendation for the operating system and development tool combinations is as follows:

  • Visual Studio 2017 on Windows 10
  • Visual Studio for Mac on macOS
  • Visual Studio Code on Windows 10 or macOS

The best version of Windows to use is Microsoft Windows 10 because you will need this version to create Universal Windows Platform apps in Chapter 17, Building Windows Apps Using XAML and Fluent Design. Earlier versions of Windows, such as 7 or 8.1, will work for the other chapters.

The best version of macOS to use is Sierra or High Sierra because you will need macOS to build iOS mobile apps in Chapter 18, Building Mobile Apps Using XAML and Xamarin.Forms. Although you can use Visual Studio 2017 on Windows to write the code for iOS and Android mobile apps, you must have macOS and Xcode to compile them.

Who this book is for

If you have heard that C# is a popular general-purpose programming language used to create every type of software, ranging from web applications and services, to business applications and games, then this book is for you.

If you have heard that C# can create software that runs on a wide range of devices, from desktop to server, from mobile to gaming systems such as Xbox One, then this book is for you.

If you have heard that .NET Core is Microsoft's bet on a cross-platform .NET future, optimized for server-side web development in the cloud, and Augmented Reality (AR) or Virtual Reality (VR) devices such as HoloLens, then this book is for you.

If you have heard that Microsoft has a popular cross-platform developer tool named Visual Studio Code that creates these cross-platform apps, and you are curious to try it, then this book is for you.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The Controllers, Models, and Views folders contain ASP.NET Core classes and the .cshtml files for execution on the server."

A block of code is set as follows:

    // storing items at index positions
    names[0] = "Kate";
    names[1] = "Jack";
    names[2] = "Rebecca";
    names[3] = "Tom";

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    // storing items at index positions
    names[0] = "Kate";
    names[1] = "Jack";
    names[2] = "Rebecca";
    names[3] = "Tom";

Any command-line input or output is written as follows:

dotnet new console

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking on the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Note

Good Practice

Recommendations for how to program like an expert appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply email [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your email address and password.
  2. Hover the mouse pointer on the SUPPORT tab at the top.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box.
  5. Select the book for which you're looking to download the code files.
  6. Choose from the drop-down menu where you purchased this book from.
  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows
  • Zipeg / iZip / UnRarX for Mac
  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/CSharp-7.1-and-.NET-Core-2.0-Modern-Cross-Platform-Development-Third-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/CSharp71andNETCore20ModernCrossPlatformDevelopmentThirdEdition_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.