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, 4th Edition [4 ed.] 9781788471572, 1788471571

Learn the fundamentals, practical applications, and latest features of C# 8.0 and .NET Core 3.0 from expert teacher Mark

2,213 614 20MB

English Pages 818 [819] Year 2019

Report DMCA / Copyright

DOWNLOAD FILE

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, 4th Edition [4 ed.]
 9781788471572, 1788471571

Table of contents :
Cover
Copyright
packt page
Contributors
Table of Contents
Preface
Chapter 1: Hello, C#! Welcome, .NET!
Setting up your development environment
Using Visual Studio Code for cross-platform development
Using Visual Studio 2019 for Windows app development
Using Visual Studio for Mac for mobile development
Recommended tools for chapters
Deploying cross-platform
Understanding Microsoft Visual Studio Code versions
Downloading and installing Visual Studio Code
Installing other extensions
Understanding .NET
Understanding the .NET Framework
Understanding the Mono and Xamarin projects
Understanding .NET Core
Understanding future versions of .NET
Understanding .NET Core support
What is different about .NET Core?
Understanding .NET Standard
.NET platforms and tools used by the book editions
Understanding intermediate language
Understanding .NET Native
Comparing .NET technologies
Building console apps using Visual Studio Code
Writing code using Visual Studio Code
Compiling and running code using dotnet CLI
Downloading solution code from a GitHub repository
Using Git with Visual Studio Code
Cloning the book solution code repository
Looking for help
Reading Microsoft documentation
Getting help for the dotnet tool
Getting definitions of types and their members
Looking for answers on Stack Overflow
Searching for answers using Google
Subscribing to the official .NET blog
Practicing and exploring
Exercise 1.1 – Test your knowledge
Exercise 1.2 – Practice C# anywhere
Exercise 1.3 – Explore topics
Summary
Chapter 2: Speaking C#
Introducing C#
Understanding language versions and features
C# 1.0
C# 2.0
C# 3.0
C# 4.0
C# 5.0
C# 6.0
C# 7.0
C# 7.1
C# 7.2
C# 7.3
C# 8.0
Discovering your C# compiler versions
Enabling a specific language version compiler
Understanding C# basics
Understanding C# grammar
Statements
Comments
Blocks
Understanding C# vocabulary
Help for writing correct code
Verbs are methods
Nouns are types, fields, and variables
Revealing the extent of the C# vocabulary
Working with variables
Naming things and assigning values
Literal values
Storing text
Understanding verbatim strings
Storing numbers
Storing whole numbers
Storing real numbers
Writing code to explore number sizes
Comparing double and decimal types
Storing Booleans
Using Visual Studio Code workspaces
Storing any type of object
Storing dynamic types
Declaring local variables
Specifying and inferring the type of a local variable
Getting default values for types
Storing multiple values
Working with null values
Making a value type nullable
Understanding nullable reference types
Enabling nullable and non-nullable reference types
Declaring non-nullable variables and parameters
Checking for null
Exploring console applications further
Displaying output to the user
Formatting using numbered positional arguments
Formatting using interpolated strings
Understanding format strings
Getting text input from the user
Importing a namespace
Simplifying the usage of the console
Getting key input from the user
Getting arguments
Setting options with arguments
Handling platforms that do not support an API
Practicing and exploring
Exercise 2.1 – Test your knowledge
Exercise 2.2 – Practice number sizes and ranges
Exercise 2.3 – Explore topics
Summary
Chapter 3: Controlling Flow and Converting Types
Operating on variables
Unary operators
Binary arithmetic operators
Assignment operators
Logical operators
Conditional logical operators
Bitwise and binary shift operators
Miscellaneous operators
Understanding selection statements
Branching with the if statement
Why you should always use braces with if statements
Pattern matching with the if statement
Branching with the switch statement
Pattern matching with the switch statement
Simplifying switch statements with switch expressions
Understanding iteration statements
Looping with the while statement
Looping with the do statement
Looping with the for statement
Looping with the foreach statement
Understanding how foreach works internally
Casting and converting between types
Casting numbers implicitly and explicitly
Converting with the System.Convert type
Rounding numbers
Understanding the default rounding rules
Taking control of rounding rules
Converting from any type to a string
Converting from a binary object to a string
Parsing from strings to numbers or dates and times
Avoiding exceptions using the TryParse method
Handling exceptions when converting types
Wrapping error-prone code in a try block
Catching all exceptions
Catching specific exceptions
Checking for overflow
Throwing overflow exceptions with the checked statement
Disabling compiler overflow checks with the unchecked statement
Practicing and exploring
Exercise 3.1 – Test your knowledge
Exercise 3.2 – Explore loops and overflow
Exercise 3.3 – Practice loops and operators
Exercise 3.4 – Practice exception handling
Exercise 3.5 – Test your knowledge of operators
Exercise 3.6 – Explore topics
Summary
Chapter 4: Writing, Debugging, and Testing Functions
Writing functions
Writing a times table function
Writing a function that returns a value
Writing mathematical functions
Converting numbers from ordinal to cardinal
Calculating factorials with recursion
Documenting functions with XML comments
Debugging during development
Creating code with a deliberate bug
Setting a breakpoint
Navigating with the debugging toolbar
Debugging windows
Stepping through code
Customizing breakpoints
Logging during development and runtime
Instrumenting with Debug and Trace
Writing to the default trace listener
Configuring trace listeners
Switching trace levels
Unit testing functions
Creating a class library that needs testing
Writing unit tests
Running unit tests
Practicing and exploring
Exercise 4.1 – Test your knowledge
Exercise 4.2 – Practice writing functions with debugging and unit testing
Exercise 4.3 – Explore topics
Summary
Chapter 5: Building Your Own Types with Object-Oriented Programming
Talking about object-oriented programming
Building class libraries
Creating a class library
Defining a class
Understanding members
Instantiating a class
Referencing an assembly
Importing a namespace to use a type
Managing multiple files
Understanding objects
Inheriting from System.Object
Storing data within fields
Defining fields
Understanding access modifiers
Setting and outputting field values
Storing a value using an enum type
Storing multiple values using an enum type
Storing multiple values using collections
Making a field static
Making a field constant
Making a field read-only
Initializing fields with constructors
Setting fields with default literals
Writing and calling methods
Returning values from methods
Combining multiple returned values using tuples
Naming the fields of a tuple
Inferring tuple names
Deconstructing tuples
Defining and passing parameters to methods
Overloading methods
Passing optional parameters and naming arguments
Controlling how parameters are passed
Splitting classes using partial
Controlling access with properties and indexers
Defining readonly properties
Defining settable properties
Defining indexers
Practicing and exploring
Exercise 5.1 – Test your knowledge
Exercise 5.2 – Explore topics
Summary
Chapter 6: Implementing Interfaces and Inheriting Classes
Setting up a class library and console application
Simplifying methods
Implementing functionality using methods
Implementing functionality using operators
Implementing functionality using local functions
Raising and handling events
Calling methods using delegates
Defining and handling delegates
Defining and handling events
Implementing interfaces
Common interfaces
Comparing objects when sorting
Comparing objects using a separate class
Defining interfaces with default implementations
Making types safely reusable with generics
Working with generic types
Working with generic methods
Managing memory with reference and value types
Working with struct types
Releasing unmanaged resources
Ensuring that Dispose is called
Inheriting from classes
Extending classes
Hiding members
Overriding members
Preventing inheritance and overriding
Understanding polymorphism
Casting within inheritance hierarchies
Implicit casting
Explicit casting
Avoiding casting exceptions
Inheriting and extending .NET types
Inheriting exceptions
Extending types when you can't inherit
Using static methods to reuse functionality
Using extension methods to reuse functionality
Practicing and exploring
Exercise 6.1 – Test your knowledge
Exercise 6.2 – Practice creating an inheritance hierarchy
Exercise 6.3 – Explore topics
Summary
Chapter 7: Understanding and Packaging .NET Types
Introducing .NET Core 3.0
.NET Core 1.0
.NET Core 1.1
.NET Core 2.0
.NET Core 2.1
.NET Core 2.2
.NET Core 3.0
Understanding .NET Core components
Understanding assemblies, packages, and namespaces
Understanding dependent assemblies
Understanding the Microsoft .NET Core App platform
Understanding NuGet packages
Understanding frameworks
Importing a namespace to use a type
Relating C# keywords to .NET types
Sharing code cross-platform with .NET Standard class libraries
Publishing your applications for deployment
Creating a console application to publish
Understanding dotnet commands
Creating new projects
Managing projects
Publishing a self-contained app
Decompiling assemblies
Packaging your libraries for NuGet distribution
Referencing a NuGet package
Fixing dependencies
Packaging a library for NuGet
Testing your package
Porting from .NET Framework to .NET Core
Could you port?
Should you port?
Differences between .NET Framework and .NET Core
Understanding the .NET Portability Analyzer
Using non-.NET Standard libraries
Practicing and exploring
Exercise 7.1 – Test your knowledge
Exercise 7.2 – Explore topics
Summary
Chapter 8: Working with Common .NET Types
Working with numbers
Working with big integers
Working with complex numbers
Working with text
Getting the length of a string
Getting the characters of a string
Splitting a string
Getting part of a string
Checking a string for content
Joining, formatting, and other string members
Building strings efficiently
Pattern matching with regular expressions
Checking for digits entered as text
Understanding the syntax of a regular expression
Examples of regular expressions
Splitting a complex comma-separated string
Storing multiple objects in collections
Common features of all collections
Understanding collection choices
Lists
Dictionaries
Stacks
Queues
Sets
Working with lists
Working with dictionaries
Sorting collections
Using specialized collections
Using immutable collections
Working with spans, indexes, and ranges
Using memory efficiently using spans
Identifying positions with the Index type
Identifying ranges with the Range type
Using indexes and ranges
Working with network resources
Working with URIs, DNS, and IP addresses
Pinging a server
Working with types and attributes
Versioning of assemblies
Reading assembly metadata
Creating custom attributes
Doing more with reflection
Internationalizing your code
Practicing and exploring
Exercise 8.1 – Test your knowledge
Exercise 8.2 – Practice regular expressions
Exercise 8.3 – Practice writing extension methods
Exercise 8.4 – Explore topics
Summary
Chapter 9: Working with Files, Streams, and Serialization
Managing the filesystem
Handling cross-platform environments and filesystems
Managing drives
Managing directories
Managing files
Managing paths
Getting file information
Controlling how you work with files
Reading and writing with streams
Writing to text streams
Writing to XML streams
Disposing of file resources
Compressing streams
Compressing with the Brotli algorithm
High-performance streams using pipelines
Asynchronous streams
Encoding and decoding text
Encoding strings as byte arrays
Encoding and decoding text in files
Serializing object graphs
Serializing as XML
Generating compact XML
Deserializing XML files
Serializing with JSON
High-performance JSON processing
Practicing and exploring
Exercise 9.1 – Test your knowledge
Exercise 9.2 – Practice serializing as XML
Exercise 9.3 – Explore topics
Summary
Chapter 10: Protecting Your Data and Applications
Understanding the vocabulary of protection
Keys and key sizes
IVs and block sizes
Salts
Generating keys and IVs
Encrypting and decrypting data
Encrypting symmetrically with AES
Hashing data
Hashing with the commonly used SHA256
Signing data
Signing with SHA256 and RSA
Generating random numbers
Generating random numbers for games
Generating random numbers for cryptography
What's new in cryptography
Authenticating and authorizing users
Implementing authentication and authorization
Protecting application functionality
Practicing and exploring
Exercise 10.1 – Test your knowledge
Exercise 10.2 – Practice protecting data with encryption and hashing
Exercise 10.3 – Practice protecting data with decryption
Exercise 10.4 – Explore topics
Summary
Chapter 11: Working with Databases Using Entity Framework Core
Understanding modern databases
Understanding Entity Framework
Using a sample relational database
Setting up SQLite for macOS
Setting up SQLite for Windows
Creating the Northwind sample database for SQLite
Managing the Northwind sample database with SQLiteStudio
Setting up EF Core
Choosing an EF Core data provider
Connecting to the database
Defining EF Core models
EF Core conventions
EF Core annotation attributes
EF Core Fluent API
Understanding data seeding
Building an EF Core model
Defining the Category and Product entity classes
Defining the Northwind database context class
Querying EF Core models
Filtering and sorting products
Logging EF Core
Logging with query tags
Pattern matching with Like
Defining global filters
Loading patterns with EF Core
Eager loading entities
Enabling lazy loading
Explicit loading entities
Manipulating data with EF Core
Inserting entities
Updating entities
Deleting entities
Pooling database contexts
Transactions
Defining an explicit transaction
Practicing and exploring
Exercise 11.1 – Test your knowledge
Exercise 11.2 – Practice exporting data using different serialization formats
Exercise 11.3 – Explore the EF Core documentation
Summary
Chapter 12: Querying and Manipulating Data Using LINQ
Writing LINQ queries
Extending sequences with the Enumerable class
Filtering entities with Where
Targeting a named method
Simplifying the code by removing the explicit delegate instantiation
Targeting a lambda expression
Sorting entities
Sorting by a single property using OrderBy
Sorting by a subsequent property using ThenBy
Filtering by type
Working with sets and bags using LINQ
Using LINQ with EF Core
Building an EF Core model
Filtering and sorting sequences
Projecting sequences into new types
Joining and grouping sequences
Aggregating sequences
Sweetening LINQ syntax with syntactic sugar
Using multiple threads with parallel LINQ
Creating an app that benefits from multiple threads
Using Windows 10
Using macOS
For all operating systems
Creating your own LINQ extension methods
Working with LINQ to XML
Generating XML using LINQ to XML
Reading XML using LINQ to XML
Practicing and exploring
Exercise 12.1 – Test your knowledge
Exercise 12.2 – Practice querying with LINQ
Exercise 12.3 – Explore topics
Summary
Chapter 13: Improving Performance and Scalability Using Multitasking
Understanding processes, threads, and tasks
Monitoring performance and resource usage
Evaluating the efficiency of types
Monitoring performance and memory use
Implementing the Recorder class
Measuring the efficiency of processing strings
Running tasks asynchronously
Running multiple actions synchronously
Running multiple actions asynchronously using tasks
Waiting for tasks
Continuing with another task
Nested and child tasks
Synchronizing access to shared resources
Accessing a resource from multiple threads
Applying a mutually exclusive lock to a resource
Understanding the lock statement and avoiding deadlocks
Making CPU operations atomic
Applying other types of synchronization
Understanding async and await
Improving responsiveness for console apps
Improving responsiveness for GUI apps
Improving scalability for web applications and web services
Common types that support multitasking
Using await in catch blocks
Working with async streams
Practicing and exploring
Exercise 13.1 – Test your knowledge
Exercise 13.2 – Explore topics
Summary
Chapter 14: Practical Applications of C# and .NET
Understanding app models for C# and .NET
Building websites using ASP.NET Core
Building websites using a web content management system
Understanding web applications
Building and consuming web services
Building intelligent apps
New features for ASP.NET Core
ASP.NET Core 1.0
ASP.NET Core 1.1
ASP.NET Core 2.0
ASP.NET Core 2.1
ASP.NET Core 2.2
ASP.NET Core 3.0
Understanding SignalR
Understanding Blazor
JavaScript and friends
Silverlight – C# and .NET using a plugin
WebAssembly – a target for Blazor
Blazor on the server-side or client-side
Understanding the bonus chapters
Building Windows desktop apps
Building cross-platform mobile apps
Building an entity data model for Northwind
Creating a class library for Northwind entity models
Creating a class library for a Northwind database context
Summary
Chapter 15: Building Websites Using ASP.NET Core Razor Pages
Understanding web development
Understanding HTTP
Client-side web development
Understanding ASP.NET Core
Classic ASP.NET versus modern ASP.NET Core
Creating an ASP.NET Core project
Testing and securing the website
Enabling static and default files
Exploring Razor Pages
Enabling Razor Pages
Defining a Razor Page
Using shared layouts with Razor Pages
Using code-behind files with Razor Pages
Using Entity Framework Core with ASP.NET Core
Configure Entity Framework Core as a service
Manipulating data using Razor pages
Enabling a model to insert entities
Defining a form to insert new suppliers
Using Razor class libraries
Using a Razor class library
Practicing and exploring
Exercise 15.1 – Test your knowledge
Exercise 15.2 – Practice building a data-driven web page
Exercise 15.3 – Explore topics
Summary
Chapter 16: Building Websites Using the Model-View-Controller Pattern
Setting up an ASP.NET Core MVC website
Creating and exploring an ASP.NET Core MVC website
Reviewing the ASP.NET Core MVC website
Reviewing the ASP.NET Core Identity database
Exploring an ASP.NET Core MVC website
Understanding ASP.NET Core MVC startup
Understanding the default MVC route
Understanding controllers and actions
Understanding filters
Using a filter to secure an action method
Using a filter to cache a response
Using a filter to define a custom route
Understanding entity and view models
Understanding views
Customizing an ASP.NET Core MVC website
Defining a custom style
Setting up the category images
Understanding Razor syntax
Defining a typed view
Testing the customized home page
Passing parameters using a route value
Understanding model binders
Validating the model
Understanding view helper methods
Querying a database and using display templates
Improving scalability using asynchronous tasks
Making controller action methods asynchronous
Using other project templates
Installing additional template packs
Practicing and exploring
Exercise 16.1 – Test your knowledge
Exercise 16.2 – Practice implementing MVC by implementing a category detail page
Exercise 16.3 – Practice improving scalability by understanding and implementing async action methods
Exercise 16.4 – Explore topics
Summary
Chapter 17: Building Websites Using a Content Management System
Understanding the benefits of a CMS
Understanding basic CMS features
Understanding enterprise CMS features
Understanding CMS platforms
Understanding Piranha CMS
Creating and exploring a Piranha CMS website
Editing site and page content
Creating a new top-level page
Creating a new child page
Reviewing the blog archive
Exploring authentication and authorization
Exploring configuration
Testing the new content
Understanding routing
Understanding media
Understanding the application service
Understanding content types
Understanding component types
Understanding standard fields
Reviewing some content types
Understanding standard blocks
Reviewing component types and standard blocks
Defining components, content types, and templates
Reviewing the standard page type
Reviewing the blog archive page type
Defining custom content and component types
Creating custom regions
Creating an entity data model
Creating custom page types
Creating custom view models
Defining custom content templates for content types
Configuring start up and importing from a database
Testing the Northwind CMS website
Uploading images and creating the catalog root
Importing category and product content
Managing catalog content
Reviewing how Piranha stores content
Practicing and exploring
Exercise 17.1 – Test your knowledge
Exercise 17.2 – Practice defining a block type for rendering YouTube videos
Exercise 17.3 – Explore topics
Summary
Chapter 18: Building and Consuming Web Services
Building web services using ASP.NET Core Web API
Understanding web service acronyms
Creating an ASP.NET Core Web API project
Reviewing the web service's functionality
Creating a web service for the Northwind database
Creating data repositories for entities
Implementing a Web API controller
Configuring the customers repository and Web API controller
Specifying problem details
Documenting and testing web services
Testing GET requests using a browser
Testing HTTP requests with REST Client extension
Enabling Swagger
Testing requests with Swagger UI
Consuming services using HTTP clients
Understanding HttpClient
Configuring HTTP clients using HttpClientFactory
Enabling Cross-Origin Resource Sharing
Implementing advanced features
Implementing Health Check API
Implementing Open API analyzers and conventions
Understanding endpoint routing
Configuring endpoint routing
Understanding other communication technologies
Understanding Windows Communication Foundation (WCF)
Understanding gRPC
Practicing and exploring
Exercise 18.1 – Test your knowledge
Exercise 18.2 – Practice creating and deleting customers with HttpClient
Exercise 18.3 – Explore topics
Summary
Chapter 19: Building Intelligent Apps Using Machine Learning
Understanding machine learning
Understanding the machine learning life cycle
Understanding datasets for training and testing
Understanding machine learning tasks
Understanding Microsoft Azure Machine Learning
Understanding ML.NET
Understanding Infer.NET
Understanding ML.NET learning pipelines
Understanding model training concepts
Understanding missing values and key types
Understanding features and labels
Making product recommendations
Problem analysis
Data gathering and processing
Creating the NorthwindML website project
Creating the data and view models
Implementing the controller
Training the recommendation models
Implementing a shopping cart with recommendations
Testing the product recommendations website
Practicing and exploring
Exercise 19.1 – Test your knowledge
Exercise 19.2 – Practice with samples
Exercise 19.3 – Explore topics
Summary
Chapter 20: Building Windows Desktop Apps
Understanding legacy Windows application platforms
Understanding .NET Core 3.0 support for legacy Windows platforms
Installing Microsoft Visual Studio 2019 for Windows
Working with Windows Forms
Building a new Windows Forms application
Reviewing a new Windows Forms application
Migrating a legacy Windows Forms application
Migrating a Windows Forms app
Migrating WPF apps to .NET Core 3.0
Migrating legacy apps using the Windows Compatibility Pack
Understanding the modern Windows platform
Understanding Universal Windows Platform
Understanding Fluent Design System
Filling user interface elements with acrylic brushes
Connecting user interface elements with animations
Parallax views and Reveal lighting
Understanding XAML Standard
Simplifying code using XAML
Choosing common controls
Understanding markup extensions
Creating a modern Windows app
Enabling developer mode
Creating a UWP project
Exploring common controls and acrylic brushes
Exploring Reveal
Installing more controls
Using resources and templates
Sharing resources
Replacing a control template
Using data binding
Binding to elements
Creating an HTTP service to bind to
Downloading the web service's certificate
Binding to data from a secure HTTP service
Creating the user interface to call the HTTP service
Converting numbers to images
Testing the HTTP service data binding
Practicing and exploring
Exercise 20.1 – Test your knowledge
Exercise 20.2 – Explore topics
Summary
Chapter 21: Building Cross-Platform Mobile Apps Using Xamarin.Forms
Understanding Xamarin and Xamarin.Forms
How Xamarin.Forms extends Xamarin
Mobile first, cloud first
Understanding additional functionality
Understanding the INotificationPropertyChanged interface
Understanding dependency services
Understanding Xamarin.Forms user interface components
Understanding the ContentPage view
Understanding the Entry and Editor controls
Understanding the ListView control
Building mobile apps using Xamarin.Forms
Adding Android SDKs
Creating a Xamarin.Forms solution
Creating an entity model with two-way data binding
Creating a component for dialing phone numbers
Creating views for the customers list and customer details
Implementing the customer list view
Implementing the customer detail view
Setting the main page for the mobile app
Testing the mobile app
Consuming a web service from a mobile app
Configuring the web service to allow insecure requests
Configuring the iOS app to allow insecure connections
Adding NuGet packages for consuming a web service
Getting customers from the web service
Practicing and exploring
Exercise 21.1 – Test your knowledge
Exercise 21.2 - Explore topics
Summary
Epilogue
Other Books You May Enjoy
Index

Polecaj historie