You are previewing C# 6 and .NET Core 1.0.
O'Reilly logo
C# 6 and .NET Core 1.0

Book Description

Create powerful cross-platform applications using C# 6, .NET Core 1.0, ASP.NET Core 1.0, and Visual Studio 2015

About This Book

  • Build modern, cross-platform applications with .NET Core 1.0

  • Get up-to-speed with C#, and up-to-date with all the latest features of C# 6

  • Start creating professional web applications with ASP.NET Core 1.0

  • Who This Book Is For

    Are you struggling to get started with C#? Or maybe you're interested in the potential of the new cross-platform features that .NET Core can offer? If so, C# 6 and .NET Core 1.0 is the book for you. While you don't need to know any of the latest features of C# or .NET to get started, it would be beneficial if you have some programming experience.

    What You Will Learn

  • Build cross-platform applications using C# 6 and .NET Core 1.0

  • Explore ASP.NET Core 1.0 and learn how to create professional web applications

  • Improve your application’s performance using multitasking

  • Use Entity Framework Core 1.0 and learn 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

    With the release of .NET Core 1.0, you can now create applications for Mac OS X and Linux, as well as Windows, using the development tools you know and love. C# 6 and .NET Core 1.0 has been divided into three high-impact sections to help start putting these new features to work.

    First, we'll run you through the basics of C#, as well as object-orient programming, before taking a quick tour through the latest features of C# 6 such as string interpolation for easier variable value output, exception filtering, and how to perform static class imports. We'll also cover both the full-feature, mature .NET Framework and the new, cross-platform .NET Core.

    After quickly taking you through C# and how .NET works, we'll dive into the internals of the .NET class libraries, covering topics such as performance, monitoring, debugging, internationalization, serialization, and encryption. We'll look at Entity Framework Core 1.0 and how to develop Code-First entity data models, as well as how to use LINQ to query and manipulate that data.

    The final section will demonstrate the major types of applications that you can build and deploy cross-device and cross-platform. In this section, we'll cover Universal Windows Platform (UWP) apps, web applications, and web services. Lastly, we'll help you build a complete application that 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# 6 and .NET Core 1.0Modern Cross-Platform Development
      1. Table of Contents
      2. C# 6 and .NET Core 1.0
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. eBooks, discount offers, and more
          1. Why subscribe?
      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. Errata
          3. Piracy
          4. Questions
      8. 1. Hello, C#! Welcome, .NET Core!
        1. Setting up your development environment
          1. Using alternative C# IDEs
          2. Using Visual Studio 2015 on Windows 10
          3. Installing Microsoft Visual Studio 2015
            1. Choosing the default installation
            2. Choosing the custom installation
            3. Completing the installation
          4. Signing in to Visual Studio
          5. Choosing development settings
          6. Updating extensions and products
          7. Using older versions of Visual Studio
        2. Understanding .NET Framework, .NET Core, and .NET Native
          1. Understanding .NET Framework platform
          2. Understanding the Mono project
          3. Understanding the .NET Core platform
            1. Streamlining .NET
            2. The future of .NET
          4. Understanding the .NET Native platform
          5. Comparing .NET technologies
        3. Writing and compiling code using the Developer Command Prompt
          1. Writing code using Notepad
          2. Compiling code using the Developer Command Prompt
          3. Fixing compiler errors
          4. Decompiling code using ILDASM
          5. Disassembling compiled assemblies
        4. Writing and compiling code using Microsoft Visual Studio 2015
          1. Writing code using Visual Studio 2015
          2. Compiling code using Visual Studio
          3. Fixing mistakes with the error list
          4. Experimenting with C# Interactive
          5. Other useful windows
        5. Targeting the .NET Core
          1. Understanding the .NET Portability Analyzer
            1. Installing the .NET Portability Analyzer
            2. Configuring the .NET Portability Analyzer
            3. Analyzing a solution
            4. .NET Portability and this book
          2. Creating new projects for the .NET Core
            1. Managing .NET Core development with Command Line Tools
            2. Installing the .NET Command Line Tools
            3. Creating a .NET Core application using Command Line Tools
            4. Creating a .NET Core application using Visual Studio 2015
        6. Managing source code with GitHub
          1. Using the Team Explorer window
          2. Cloning a GitHub repository
          3. Managing a GitHub repository
        7. Practicing and exploring
          1. Exercise 1.1 – test your knowledge
          2. Exercise 1.2 – practice managing Visual Studio Windows
          3. Exercise 1.3 – practice coding anywhere
          4. Exercise 1.4 – explore topics
        8. Summary
      9. 2. Speaking C#
        1. Understanding C# basics
          1. The C# grammar
            1. Statements
            2. Blocks
          2. The C# vocabulary
          3. Writing the code
          4. Verbs are methods
          5. Nouns are types, fields, and variables
          6. Counting types and methods
        2. Declaring variables
          1. Naming variables
          2. Storing text
          3. Storing numbers
            1. Storing whole numbers
            2. Storing real numbers
              1. Sizes of numbers in memory
              2. Comparing double and decimal
          4. Storing Booleans
          5. The object type
          6. The dynamic type
          7. Local variables
            1. Inferring the type of a local variable
          8. Making a value type nullable
          9. 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 in C# 6
          5. Reading arguments and working with arrays
        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. The if-else statement
          2. The switch-case statement
          3. Autoformatting code
        2. Iteration statements
          1. The while statement
          2. The do-while statement
          3. The for statement
          4. The foreach statement
            1. How does the foreach statement actually work?
        3. Casting and converting between types
          1. Casting from numbers to numbers
          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-catch 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. MSDN
          2. Getting the definition of code
          3. StackOverflow
          4. Google
          5. Design patterns
        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 Common .NET Types
        1. Using assemblies and namespaces
          1. Comparing .NET Framework with .NET Core
          2. Base Class Libraries and CoreFX
            1. Assemblies
            2. Namespaces
          3. Referencing an assembly
          4. Referencing Microsoft Core Library
          5. An example of assembly references
          6. Relating assemblies and namespaces
          7. Importing a namespace
          8. Browsing assemblies and namespaces
          9. 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. Extracting part of a string
          5. Checking a string for content
          6. Other string members
          7. Building strings efficiently
          8. Validating input with regular expressions
          9. The syntax of a regular expression
          10. 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. Avoiding old collections
          6. Using specialized collections
        4. Practicing and exploring
          1. Exercise 4.1 – test your knowledge
          2. Exercise 4.2 – practice regular expressions
          3. Exercise 4.3 – explore topics
        5. Summary
      12. 5. Using Specialized .NET Types
        1. Debugging and diagnostics
          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
            3. Measuring the efficiency of processing strings
          3. Monitoring with Debug and Trace
            1. Writing to the default trace listener
            2. Configuring trace listeners
            3. Configuring compiler symbols for .NET Framework
            4. Defining compiler symbols for .NET Core
            5. Checking compiler symbols
            6. Switching trace levels
        2. Unit testing an application
          1. Creating a unit of code that needs testing
          2. Creating a unit test project
          3. Running unit tests
        3. Internationalizing an application
          1. Globalizing an application
          2. Localizing an application
        4. Interoperating with unmanaged code
          1. Automating Microsoft Excel using COM Interop
          2. Accessing the Win32 API with P/Invoke
        5. Practicing and exploring
          1. Exercise 5.1 – test your knowledge
          2. Exercise 5.2 – practice using Debug and Trace
          3. Exercise 5.3 – explore topics
        6. Summary
      13. 6. Building Your Own Types with Object-Oriented Programming
        1. Talking about OOP
        2. Building class libraries
          1. Creating a class library to share code
          2. Defining a class
          3. Instantiating a class
        3. Storing data with fields
          1. Defining fields
          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. Overloading methods
          2. Optional parameters and named arguments
          3. Splitting classes using partial
        5. Controlling access with properties and indexers
          1. Defining read-only properties
          2. Defining settable properties
          3. Defining indexers
        6. Simplifying methods with operators
        7. Raising and handling events
          1. Calling methods using delegates
          2. Defining events
        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. Implementing interfaces
          1. Common interfaces
          2. Comparing objects when sorting
          3. Defining a separate comparer
        2. Managing memory with reference and value types
          1. Defining a type using the struct keyword
          2. Releasing unmanaged resources
          3. Ensuring that dispose is called
        3. Inheriting from classes
          1. Extending classes
          2. Hiding members
          3. Overriding members
          4. Preventing inheritance and overriding
          5. Polymorphism
        4. Casting within inheritance hierarchies
          1. Implicit casting
          2. Explicit casting
          3. Handling casting exceptions
        5. Documenting your types
        6. Inheriting and extending .NET types
          1. Inheriting from the Exception class
          2. Extending types when you can't inherit
            1. Using static methods to reuse functionality
            2. Using extension methods to reuse functionality
        7. 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
        8. Summary
      15. 8. Working with Relational Data Using the Entity Framework
        1. Relational Database Management Systems
          1. Connecting to Microsoft SQL Server LocalDb
          2. The Northwind sample database
          3. Choosing a .NET data provider
        2. Using ADO.NET
          1. Connecting to the database
          2. Executing commands and reading result sets
          3. Loading a connection string from configuration
        3. Using Entity Framework 6
          1. Building an Entity Framework 6 model
          2. Entity Framework Code First models
            1. EF Code First conventions
            2. EF annotation attributes
            3. EF Fluent API
          3. Querying an Entity Data Model
          4. Logging SQL statements
          5. Manipulating data with Entity Data Models
            1. Inserting entities
            2. Updating entities
            3. Transactions
            4. Defining an explicit transaction
          6. Loading patterns with EF
            1. Lazy loading entities
            2. Eager loading entities
            3. Explicit loading entities
        4. Using Entity Framework Core
        5. Practicing and exploring
          1. Exercise 8.1 – test your knowledge
          2. Exercise 8.2 – explore the EF Core documentation
          3. Exercise 8.3 – explore topics
        6. 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 sequences with OrderBy
            1. Sorting by multiple properties with the ThenBy method
          4. Working with sets
          5. Projecting entities with Select statement
          6. Joining and grouping
          7. Sweetening the syntax with syntactic sugar
        2. Using multiple threads with Parallel LINQ
        3. Creating your own LINQ extension methods
        4. Working with LINQ to XML
          1. Generating XML using LINQ to XML
          2. Reading XML by using LINQ to XML
        5. Practicing and exploring
          1. Exercise 9.1 – test your knowledge
          2. Exercise 9.2 – practice querying with LINQ
          3. Exercise 9.3 – explore topics
        6. 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
          6. Serializing with runtime serializers
        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 Microsoft's System.IO types
          5. Exercise 10.5 – explore topics
        6. Summary
      18. 11. Protecting Your Data and Applications
        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
        3. Hashing data
          1. Hashing with SHA256
        4. Signing data
          1. Signing with SHA256 and RSA
        5. Authenticating and authorizing users
          1. Managing local accounts
          2. Authenticating with Windows
          3. Authorizing with Windows
        6. 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
        7. Summary
      19. 12. Improving Performance and Scalability with Multitasking
        1. Understanding processes and threads
        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. The async and await keywords
          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
        4. 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
        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 the UWP
          1. Adapting your app's layout
          2. Taking advantage of unique device capabilities
        2. Understanding XAML
          1. Simplifying code using XAML
        3. Creating a Windows desktop application using WPF
          1. Choosing common controls
        4. Creating an app for the Universal Windows Platform
          1. Analyzing UWP portability
        5. Using resources and templates

          1. Sharing resources
          2. Replacing a control template
        6. Data binding
          1. Binding to elements
          2. Binding to data
        7. Animating with storyboards
        8. Testing in emulators
        9. 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
        10. Summary
      21. 14. Building Web Applications and Services Using ASP.NET Core
        1. Understanding ASP.NET Core
          1. Classic ASP.NET versus modern ASP.NET Core
          2. Client-side web development
            1. Install Web Essentials 2015
          3. The HyperText Transfer Protocol (HTTP)
          4. Create a web application project
          5. Exploring the parts of an ASP.NET Core web application
        2. ASP.NET Core startup
          1. Understanding the default route
        3. ASP.NET Core controllers
          1. Defining the Home controller's actions
        4. ASP.NET Core 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 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 further
          1. Passing parameters using a route value
          2. Passing parameters using a query string
          3. Annotating models
        7. ASP.NET Core Web API
          1. Scaffolding an API controller
          2. Calling a Web API service from a UWP app
        8. 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
        9. Summary
      22. 15. Taking C# Cross-Platform
        1. Understanding cross-platform development
          1. Visual Studio Code
          2. .NET Core 1.0
          3. ASP.NET Core 1.0
          4. Entity Framework Core 1.0
          5. .NET Native
          6. Xamarin
          7. Cross-device Universal Windows Platform (UWP)
        2. Installing Visual Studio Code and ASP.NET Core
          1. Installing Visual Studio Code
          2. Managing development from the terminal
          3. Installing Homebrew
          4. Installing ASP.NET Core and the .NET Version Manager
          5. Listing the installed versions of .NET
          6. Switching from Mono to .NET Core
        3. Building a console application
          1. Creating the source files
          2. Editing the code
          3. Downloading dependency packages and compiling
        4. Building cross-platform web applications using ASP.NET Core
          1. Creating the simplest ASP.NET Core web application
          2. Installing Yeoman and related tools
          3. Scaffolding projects using Yeoman
          4. Editing projects using Visual Studio Code
        5. Understanding the .NET Core command-line tools
          1. Understanding the DNX tools
          2. Understanding the CLI tools
          3. Common DNX commands
          4. Common CLI commands
        6. Practicing and exploring
          1. Exercise 15.1 – test your knowledge
          2. Exercise 15.2 – practice transferring an existing ASP.NET application
          3. Exercise 15.3 – explore topics
        7. Summary
      23. 16. Building a Quiz
        1. Designing the quiz application
          1. Choosing the platform
          2. Deciding the functional requirements
          3. Separating concerns
        2. Building the quiz solution
          1. Defining the entity models
          2. Creating the data repository
          3. Creating the web application
          4. Defining view models
          5. Adding sample quiz questions
          6. Configuring session state
          7. Adding custom controller actions
          8. Adding custom views
        3. Running the quiz
          1. Configuring the project to use Kestrel and .NET Core
        4. Hosting ASP.NET Core web apps in Microsoft Azure
          1. Register an Azure account
          2. Create an Azure web app
          3. Publishing an ASP.NET web application to the Web App
        5. Practicing and exploring
          1. Exercise 16.1 – test your knowledge
          2. Exercise 16.2 – practice by extending the quiz web app
          3. Exercise 16.3 – explore topics
        6. 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
          1. Exercise 3.2
        4. Chapter 4 – Using Common .NET Types
        5. Chapter 5 – Using Specialized .NET Types
        6. Chapter 6 – Building Your Own Types with Object-Oriented Programming
        7. Chapter 7 – Implementing Interfaces and Inheriting Classes
        8. Chapter 8 – Working with Relational Data Using the Entity Framework
        9. Chapter 9 – Querying and Manipulating Data with LINQ
        10. Chapter 10 – Working with Files, Streams, and Serialization
        11. Chapter 11 – Protecting Your Data and Applications
        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 and Services Using ASP.NET Core
        15. Chapter 15 – Taking C# Cross-Platform
        16. Chapter 16 – Building a Quiz
      25. B. Creating a Virtual Machine for Your Development Environment
        1. Signing up for a Microsoft account
        2. Creating a Microsoft Azure subscription
        3. Managing your Microsoft Azure account
        4. Creating a virtual machine
        5. Connecting to your virtual machine using remote desktop
        6. Supporting other platforms
        7. Summary
      26. Index