O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition - Get up to speed with C#, and up to date with all the latest features of C# 7

Book Description

Modern Cross-Platform Development

About This Book

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

Who This Book Is For

This book is targeted towards readers who have some prior programming experience or have a science, technology, engineering, or mathematics (STEM) background, and want to gain a solid foundation with C# and to be introduced to the types of applications they could build and will work cross-platform on Windows, Linux, and macOS.

What You Will Learn

  • Build cross-platform applications using C# 7 and .NET Core
  • Explore ASP.NET Core and learn how to create professional web applications
  • Improve your application’s performance using multitasking
  • Use Entity Framework Core and find out how to build code-first databases
  • Master object-oriented programming with C# to increase code reuse and efficiency
  • Familiarize yourself with cross-device app development using the Universal Windows Platform and XAML
  • Query and manipulate data using LINQ
  • Protect your data by using encryption and hashing

In Detail

If you want to build powerful cross-platform applications with C# 7 and .NET Core, then this book is for you.

First, we’ll run you through the basics of C#, as well as object-oriented programming, before taking a quick tour through the latest features of C# 7 such as tuples, pattern matching, out variables, and so on.

After quickly taking you through C# and how .NET works, we’ll dive into the .NET Standard 1.6 class libraries, covering topics such as performance, monitoring, debugging, serialization and encryption.

The final section will demonstrate the major types of application that you can build and deploy cross-device and cross-platform. In this section, we’ll cover Universal Windows Platform (UWP) apps, web applications, mobile apps, and web services. Lastly, we’ll look at how you can package and deploy your applications so that they can be hosted on all of today’s most popular platforms, including Linux and Docker.

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 Core.

Style and approach

This book takes a step-by-step approach and is filled with exciting projects and fascinating theory. It uses three high-impact sections to equip you with all the tools you’ll need to build modern, cross-platform applications using C# and .NET Core.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

Table of Contents

  1. C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition
    1. C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
    6. Customer Feedback
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Hello, C#! Welcome, .NET Core!
      1. Choosing your development environment
        1. Using alternative C# IDEs
        2. Deploying cross-platform
      2. Installing Microsoft Visual Studio 2017 for Windows
        1. Choosing workloads
        2. Choosing additional components
        3. Signing in to Visual Studio 2017
        4. Choosing your settings
        5. Reviewing Visual Studio's user interface
      3. Installing Microsoft Visual Studio Code for Windows, macOS, or Linux
        1. Installing Microsoft Visual Studio Code for macOS
        2. Installing .NET Core for macOS
          1. Installing Homebrew
          2. Installing OpenSSL
          3. Installing .NET Core SDK
        3. Installing the Visual Studio Code extension for C#
      4. Understanding .NET
        1. Understanding .NET Framework
        2. Understanding the Mono and Xamarin projects
        3. Understanding .NET Core
          1. Streamlining .NET
        4. Understanding .NET Standard
          1. .NET used in this book
        5. Understanding .NET Native
        6. Comparing .NET technologies
      5. Writing and compiling code using the .NET Core CLI tool
        1. Writing code using a simple text editor
          1. If you are using Windows Notepad
          2. If you are using macOS TextEdit
        2. Compiling code using the .NET Core CLI tool
          1. Restoring packages and running the application
        3. Fixing compiler errors
        4. Understanding Intermediate Language
      6. Writing and compiling code using Microsoft Visual Studio 2017
        1. Writing code using Microsoft Visual Studio 2017
          1. Coding with Visual Studio's editor
        2. Compiling code using Visual Studio 2017
        3. Fixing mistakes with the error list
        4. Adding existing projects to Visual Studio 2017
        5. Autoformatting code
        6. Experimenting with C# Interactive
        7. Other useful windows
      7. Writing and compiling code using Microsoft Visual Studio Code
        1. Writing code using Visual Studio Code
        2. Compiling code using Visual Studio Code
      8. Managing source code with GitHub
        1. Using Git with Visual Studio 2017
          1. Using the Team Explorer window
          2. Cloning a GitHub repository
          3. Managing a GitHub repository
        2. Using Git with Visual Studio Code
          1. Configuring Git at the command line
          2. Managing Git with Visual Studio Code
      9. Practicing and exploring
        1. Exercise 1.1 - test your knowledge
        2. Exercise 1.2 - practice coding anywhere
        3. Exercise 1.3 - explore topics
      10. Summary
    9. 2. Speaking C#
      1. Understanding C# basics
        1. Using Visual Studio 2017
        2. Using Visual Studio Code on macOS, Linux, or Windows
        3. C# grammar
          1. Statements
          2. Comments
          3. Blocks
        4. C# vocabulary
          1. C# keywords
        5. Writing the code
        6. Verbs are methods
        7. Nouns are types, fields, and variables
        8. Counting types and methods
          1. Building and running with Visual Studio 2017
          2. Building and running with Visual Studio Code
      2. Declaring variables
        1. Naming variables
        2. Literal values
        3. Storing text
        4. Storing numbers
          1. Storing whole numbers
          2. C# 7 improvements
          3. Storing real numbers
            1. Using Visual Studio 2017
            2. Using Visual Studio Code
          4. Writing code to explore numbers
            1. Comparing double and decimal
        5. Storing Booleans
        6. The object type
        7. The dynamic type
        8. Local variables
          1. Inferring the type of a local variable
        9. Making a value type nullable
          1. Checking for null
        10. Storing multiple values in an array
      3. Building console applications
        1. Displaying output to the user
        2. Getting input from the user
        3. Importing a namespace
        4. Simplifying the usage of the console
        5. Reading arguments and working with arrays
          1. Passing arguments with Visual Studio 2017
          2. Passing arguments with Visual Studio Code
          3. Viewing the output
          4. Enumerating arguments
            1. Running on Windows
            2. Running on macOS
          5. Handling platforms that do not support an API
      4. Operating on variables
        1. Experimenting with unary operators
        2. Experimenting with arithmetic operators
        3. Comparison and Boolean operators
      5. Practicing and exploring
        1. Exercise 2.1 - test your knowledge
        2. Exercise 2.2 - practice number sizes and ranges
        3. Exercise 2.3 - explore topics
      6. Summary
    10. 3. Controlling the Flow, Converting Types, and Handling Exceptions
      1. Selection statements
        1. Using Visual Studio 2017
        2. Using Visual Studio Code on macOS, Linux, or Windows
        3. The if statement
          1. Pattern matching with the if statement
        4. The switch statement
          1. Pattern matching with the switch statement
      2. Iteration statements
        1. The while statement
        2. The do statement
        3. The for statement
        4. The foreach statement
          1. How does the foreach statement work?
      3. Casting and converting between types
        1. Casting from numbers to numbers
          1. Casting explicitly
        2. Using the convert type
        3. Rounding numbers
        4. Converting from any type to a string
        5. Parsing from strings to numbers or dates and times
      4. Handling exceptions
        1. The try statement
          1. Catching all exceptions
          2. Catching specific exceptions
        2. The finally statement
          1. Simplifying disposal with the using statement
      5. Checking for overflow
        1. The checked statement
        2. The unchecked statement
      6. Looking for help
        1. Microsoft Docs and MSDN
        2. Go to definition
        3. StackOverflow
        4. Google
        5. Subscribing to blogs
        6. Design patterns
          1. Singleton pattern
      7. Practice and explore
        1. Exercise 3.1 - test your knowledge
        2. Exercise 3.2 - explore loops and overflow
        3. Exercise 3.3 - practice loops and operators
        4. Exercise 3.4 - practice exception handling
        5. Exercise 3.5 - explore topics
      8. Summary
    11. 4. Using .NET Standard Types
      1. Using assemblies and namespaces
        1. Comparing .NET Framework with .NET Core
        2. Base Class Libraries and CoreFX
          1. Assemblies, NuGet packages, and platforms
          2. Namespaces
        3. Referencing a dependent assembly
        4. Browsing assemblies with Visual Studio 2017
        5. Understanding .NET Core project files
        6. Relating assemblies and namespaces
        7. Importing a namespace
        8. Relating C# keywords to .NET types
      2. Storing and manipulating text
        1. Getting the length of a string
        2. Getting the characters of a string
        3. Splitting a string
        4. Getting part of a string
        5. Checking a string for content
        6. Other string members
        7. Building strings efficiently
        8. Pattern matching with regular expressions
          1. The syntax of a regular expression
        9. Examples of regular expressions
      3. Storing data with collections
        1. Understanding collections
          1. Lists
          2. Dictionaries
          3. Stacks
          4. Queues
          5. Sets
        2. Working with lists
        3. Working with dictionaries
        4. Sorting collections
        5. Using specialized collections
      4. Internationalizing an application
        1. Globalizing an application
      5. Practicing and exploring
        1. Exercise 4.1 - test your knowledge
        2. Exercise 4.2 - practice regular expressions
        3. Exercise 4.3 - explore topics
      6. Summary
    12. 5. Debugging, Monitoring, and Testing
      1. Debugging tools
        1. Debugging an application
          1. Setting a breakpoint
          2. The debugging toolbar
          3. Debugging windows
          4. Stepping through code
          5. Customizing breakpoints
      2. Monitoring performance and resource usage
        1. Evaluating the efficiency of types
        2. Monitoring performance and memory use
          1. Measuring the efficiency of processing strings
      3. Unit testing an application
        1. Creating a class library that needs testing with Visual Studio 2017
        2. Creating a unit test project with Visual Studio 2017
        3. Creating a class library that needs testing with Visual Studio Code
        4. Writing unit tests
        5. Running unit tests with Visual Studio 2017
        6. Running unit tests with Visual Studio Code
      4. Practicing and exploring
        1. Exercise 5.1 - test your knowledge
        2. Exercise 5.2 - practice debugging and unit testing
        3. Exercise 5.3 - explore topics
      5. Summary
    13. 6. Building Your Own Types with Object-Oriented Programming
      1. Talking about OOP
      2. Building class libraries
        1. Creating a class library with Visual Studio 2017
        2. Creating a class library with Visual Studio Code
        3. Defining a class
        4. Instantiating a class
          1. Referencing an assembly using Visual Studio 2017
          2. Referencing an assembly using Visual Studio Code
          3. Importing a namespace
        5. Managing multiple projects with Visual Studio Code
        6. Inheriting from System.Object
      3. Storing data with fields
        1. Defining fields
          1. Understanding access modifiers
        2. Storing a value using the enum keyword
        3. Storing multiple values using collections
        4. Making a field static
        5. Making a field constant
        6. Making a field read-only
        7. Initializing fields with constructors
      4. Writing and calling methods
        1. Combining multiple values with tuples
          1. Referencing the System.ValueTuple package with Visual Studio 2017
          2. Referencing the System.ValueTuple package with Visual Studio Code
          3. Defining methods with tuples
          4. Naming the fields of a tuple
          5. Deconstructing tuples
        2. Defining and passing parameters to methods
        3. Overloading methods
        4. Optional parameters and named arguments
      5. Controlling how parameters are passed
      6. Splitting classes using partial
      7. Controlling access with properties and indexers
        1. Defining read-only properties
        2. Defining settable properties
        3. Defining indexers
      8. Practicing and exploring
        1. Exercise 6.1 - test your knowledge
        2. Exercise 6.2 - practice writing mathematical methods
        3. Exercise 6.3 - explore topics
      9. Summary
    14. 7. Implementing Interfaces and Inheriting Classes
      1. Setting up a class library and console application
        1. Using Visual Studio 2017
        2. Using Visual Studio Code
        3. Defining the classes
      2. Simplifying methods with operators
        1. Implementing some functionality with a method
        2. Implementing some functionality with an operator
      3. Defining local functions
      4. Raising and handling events
        1. Calling methods using delegates
        2. Defining events
      5. Implementing interfaces
        1. Common interfaces
        2. Comparing objects when sorting
          1. Defining a separate comparer
      6. Managing memory with reference and value types
        1. Defining a struct type
        2. Releasing unmanaged resources
        3. Ensuring that dispose is called
      7. Inheriting from classes
        1. Extending classes
        2. Hiding members
        3. Overriding members
        4. Preventing inheritance and overriding
        5. Polymorphism
      8. Casting within inheritance hierarchies
        1. Implicit casting
        2. Explicit casting
        3. Handling casting exceptions
      9. Inheriting and extending .NET types
        1. Inheriting from an exception
        2. Extending types when you can't inherit
          1. Using static methods to reuse functionality
          2. Using extension methods to reuse functionality
      10. Practice and explore
        1. Exercise 7.1 - test your knowledge
        2. Exercise 7.2 - practice creating an inheritance hierarchy
        3. Exercise 7.3 - explore topics
      11. Summary
    15. 8. Working with Databases Using the Entity Framework Core
      1. Relational Database Management Systems
        1. Using a sample database
      2. Using Microsoft SQL Server on Windows
        1. Connecting to Microsoft SQL Server LocalDb
        2. Creating the Northwind sample database
        3. Managing the Northwind sample database
      3. Using SQLite on macOS and mobile platforms
        1. Running a script for SQLite
        2. Choosing a .NET data provider
      4. Connecting to the database
      5. Setting up Entity Framework Core
        1. Using Visual Studio 2017
        2. Using Visual Studio Code
        3. Entity Framework Core models
          1. EF Core conventions
          2. EF Core annotation attributes
          3. EF Core Fluent API
        4. Building an EF Core Model
      6. Querying an EF Core model
        1. Logging EF Core
        2. Loading patterns with EF Core
          1. Eager and lazy loading entities
          2. Explicit loading entities
      7. Manipulating data with EF Core
        1. Inserting entities
        2. Updating entities
        3. Deleting entities
        4. Transactions
        5. Defining an explicit transaction
      8. Practicing and exploring
        1. Exercise 8.1 - test your knowledge
        2. Exercise 8.2 - explore the EF Core documentation
      9. Summary
    16. 9. Querying and Manipulating Data with LINQ
      1. Writing LINQ queries
        1. Extending sequences with the enumerable class
        2. Filtering entities with Where
          1. Targeting a named method
          2. Simplifying the code by removing the explicit delegate instantiation
          3. Targeting a lambda expression
        3. Sorting entities with OrderBy
          1. Sorting by multiple properties with the ThenBy method
      2. Working with sets
      3. Projecting entities with Select
        1. Building an EF Core model
        2. Joining and grouping
      4. Sweetening the syntax with syntactic sugar
      5. Using multiple threads with parallel LINQ
      6. Creating your own LINQ extension methods
      7. Working with LINQ to XML
        1. Generating XML using LINQ to XML
        2. Reading XML by using LINQ to XML
      8. Practicing and exploring
        1. Exercise 9.1 - test your knowledge
        2. Exercise 9.2 - practice querying with LINQ
        3. Exercise 9.3 - explore topics
      9. Summary
    17. 10. Working with Files, Streams, and Serialization
      1. Managing the filesystem
        1. Managing directories
        2. Managing files
        3. Managing paths
        4. Getting file information
      2. Reading and writing with streams
        1. Writing to text and XML streams
        2. Compressing streams
      3. Encoding text
        1. Encoding strings as byte arrays
        2. Encoding and decoding text in files
      4. Serializing object graphs
        1. Serializing with XML
        2. Deserializing with XML
        3. Customizing the XML
        4. Serializing with JSON
        5. Serializing with other formats
      5. Practice and explore
        1. Exercise 10.1 - test your knowledge
        2. Exercise 10.2 - practice serializing as XML
        3. Exercise 10.3 - explore serialization formats
        4. Exercise 10.4 - explore topics
      6. Summary
    18. 11. Protecting Your Data
      1. Understanding the vocabulary of protection
        1. Keys and key sizes
        2. IVs and block sizes
        3. Salts
        4. Generating keys and IVs
      2. Encrypting and decrypting data
        1. Encrypting symmetrically with AES
          1. Using Visual Studio 2017
          2. Using Visual Studio Code
          3. Creating the Protector class
      3. Hashing data
        1. Hashing with SHA256
      4. Signing data
        1. Signing with SHA256 and RSA
      5. Practicing and exploring
        1. Exercise 11.1 - test your knowledge
        2. Exercise 11.2 - practice protecting data with encryption and hashing
        3. Exercise 11.3 - practice protecting data with decryption
        4. Exercise 11.4 - explore topics
      6. Summary
    19. 12. Improving Performance and Scalability with Multitasking
      1. Understanding processes, threads, and tasks
      2. Running tasks asynchronously
        1. Running multiple actions synchronously
        2. Running multiple actions asynchronously using tasks
        3. Waiting for tasks
        4. Continuing with another task
        5. Nested and child tasks
      3. Synchronizing access to shared resources
        1. Accessing a resource from multiple threads
        2. Applying a mutually exclusive lock to a resource
        3. Understanding the lock statement
        4. Making operations atomic
        5. Applying other types of synchronization
      4. Implementing multitasking for a GUI
        1. Creating a GUI that blocks
        2. Creating a GUI that doesn't block
        3. Other types with Async methods
        4. await in catch blocks
        5. Improving scalability for client-server applications
      5. Practicing and exploring
        1. Exercise 12.1 - test your knowledge
        2. Exercise 12.2 - explore topics
      6. Summary
    20. 13. Building Universal Windows Platform Apps Using XAML
      1. Understanding Universal Windows Platform
        1. Adapting your app's layout
        2. Taking advantage of unique device capabilities
      2. Understanding XAML
        1. Simplifying code using XAML
        2. Choosing common controls
      3. Creating an app for Universal Windows Platform
      4. Using resources and templates
        1. Sharing resources
        2. Replacing a control template
      5. Data binding
        1. Binding to elements
        2. Binding to data
      6. Animating with storyboards
      7. Testing in emulators
      8. Practicing and exploring
        1. Exercise 13.1 - test your knowledge
        2. Exercise 13.2 - practice building a universal tip calculator
        3. Exercise 13.3 - explore topics
      9. Summary
    21. 14. Building Web Applications Using ASP.NET Core MVC
      1. Understanding ASP.NET Core
        1. Classic ASP.NET versus modern ASP.NET Core
        2. Client-side web development
        3. Understanding HTTP
        4. Creating an ASP.NET Core project with Visual Studio 2017
          1. Performing database migrations
          2. Reviewing authentication with ASP.NET Identity
        5. Creating an ASP.NET Core project with Visual Studio Code
          1. Managing client-side packages with Bower
      2. Exploring an ASP.NET Core MVC web application
        1. ASP.NET Core startup
        2. Understanding the default route
      3. ASP.NET Core MVC controllers
      4. ASP.NET Core MVC models
        1. Create Entity models for Northwind
        2. Configure Entity Framework Core as a service
        3. Create view models for requests
        4. Fetch the model in the controller
      5. ASP.NET Core MVC views
        1. Rendering the Home controller's views
        2. Sharing layouts between views
        3. Defining custom styles
        4. Defining a typed view
      6. Taking ASP.NET Core MVC further
        1. Passing parameters using a route value
        2. Passing parameters using a query string
        3. Annotating models
      7. Practicing and exploring
        1. Exercise 14.1 - test your knowledge
        2. Exercise 14.2 - practice building a data-driven web application
        3. Exercise 14.3 - explore topics
      8. Summary
    22. 15. Building Mobile Apps Using Xamarin.Forms and ASP.NET Core Web API
      1. Understanding Xamarin.Forms
        1. How Xamarin.Forms extends Xamarin
        2. Mobile first, cloud first
        3. Installing Xcode
        4. Installing Visual Studio for Mac
      2. Building services using ASP.NET Core Web API and Visual Studio Code
        1. Creating an ASP.NET Core Web API project
        2. Creating a web service for the Northwind database
          1. Creating the Northwind database
          2. Referencing the EF Core NuGet packages
          3. Creating the entity model and database context
          4. Creating the data repository
          5. Configuring and registering the data repository
          6. Set the database connection string
          7. Creating the Web API controller
        3. Testing the web service
          1. Testing GET requests with any browser
          2. Testing POST, PUT, DELETE, and other requests with Postman
      3. Building mobile apps using Xamarin.Forms and Visual Studio for Mac
        1. Creating a Xamarin.Forms project
        2. Creating a model
        3. Creating an interface for dialing phone numbers and implement for iOS and Android
        4. Creating views for the customers list and customer details
        5. Test the mobile app with iOS
        6. Adding NuGet packages for calling a REST service
        7. Getting customers from the service
      4. Practicing and exploring
        1. Exercise 15.1 - test your knowledge
        2. Exercise 15.2 - explore topics
      5. Summary
    23. 16. Packaging and Deploying Your Code Cross-Platform
      1. Porting to .NET Core
        1. Could you port?
        2. Should you port?
        3. Differences between .NET Framework and .NET Core
        4. Understanding the .NET Portability Analyzer
      2. Sharing code cross-platform with .NET Standard class libraries
        1. Creating a .NET Standard class library
          1. Using Visual Studio 2017
          2. Using Visual Studio Code on macOS
      3. Understanding NuGet packages
        1. Referencing packages
        2. Understanding metapackages
        3. Understanding Frameworks
        4. Fixing dependencies
        5. Switching to a different .NET Standard
      4. Publishing your applications
        1. Creating a console application to publish
        2. Publishing with Visual Studio 2017
        3. Publishing with Visual Studio Code
      5. Deploying to the cloud
        1. Creating an ASP.NET Core MVC web application to publish
        2. Register an Azure account
        3. Create an Azure web app
        4. Publishing an ASP.NET web application to the web app
      6. Developing on and for Linux
      7. Practicing and exploring
        1. Exercise 16.1 - test your knowledge
        2. Exercise 16.2 - explore topics
      8. Summary
    24. A. Answers to the Test Your Knowledge Questions
      1. Chapter 1 - Hello, C#! Welcome, .NET Core!
      2. Chapter 2 - Speaking C#
      3. Chapter 3 - Controlling the Flow, Converting Types, and Handling Exceptions
      4. Chapter 4 - Using .NET Standard Types
      5. Chapter 5 - Debugging, Monitoring, and Testing
      6. Chapter 6 - Building Your Own Types with Object-Oriented Programming
      7. Chapter 7 - Implementing Interfaces and Inheriting Classes
      8. Chapter 8 - Working with Databases Using Entity Framework Core
      9. Chapter 9 - Querying and Manipulating Data with LINQ
      10. Chapter 10 - Working with Files, Streams, and Serialization
      11. Chapter 11 - Protecting Your Data
      12. Chapter 12 - Improving Performance and Scalability with Multitasking
      13. Chapter 13 - Building Universal Windows Platform Apps Using XAML
      14. Chapter 14 - Building Web Applications Using ASP.NET Core MVC
      15. Chapter 15 - Building Mobile Apps Using Xamarin.Forms and ASP.NET Core Web API
      16. Chapter 16 - Packaging and Deploying Your Code Cross-Platform