Book Image

C# 6 and .NET Core 1.0

Book Image

C# 6 and .NET Core 1.0

Overview of this book

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.
Table of Contents (25 chapters)
C# 6 and .NET Core 1.0
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

There are many C# books, some, more than a thousand 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 modern cross-platform C# and .NET proven practices.

I will point out the cool corners and gotchas of C# so you can impress colleagues and employers and quickly get productive. Rather than slowing down and boring some of you by explaining every little thing, I assume that if a term I use is new to you, then you know how to Google it with a search engine such as DuckDuckGo.

At the end of each chapter, there is a section entitled Practicing and Exploring, which contains questions to test your knowledge, and usually a hands-on practical exercise, and you will explore topics in depth on your own with a little nudge in the right direction from me.

You can download solutions for the exercises from the GitHub repository at https://github.com/markjprice/cs6dotnetcore. I will provide instructions on how to do this using Visual Studio 2015 at the end of Chapter 1, Hello C#!, Welcome .NET Core!.

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 compile C# code at the Command Prompt and how to write and compile code using Visual Studio. You will also learn about the different .NET platforms: .NET Framework, .NET Core, and .NET Native.

Chapter 2, Speaking C#, is about the C# language, 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, Converting Types, and Handling Exceptions, is about writing code that makes decisions, repeats blocks of statements, converts between types, and handles errors. You will also learn the best places to look for help.

Chapter 4, Using Common .NET Types, is about how .NET types are related to C#. You will learn about .NET Framework, .NET Core, and their class library assemblies of types that allow your applications to connect together existing components to perform common practical tasks.

Chapter 5, Using Specialized .NET Types, is about .NET types used to diagnose problems, support multiple languages and cultures, and access features and applications outside of .NET.

Chapter 6, Building Your Own Types with Object-Oriented Programming, is about all the different categories of members that a type can have, including fields for storing data and methods for performing actions. You will use OOP concepts such as aggregation and encapsulation.

Chapter 7, Implementing Interfaces and Inheriting Classes, is about deriving new types from existing ones using OOP. You will learn how to implement interfaces, about base and derived classes, how to override a type member, how to use polymorphism, and how to cast between classes in an inheritance hierarchy.

Chapter 8, Working with Relational Data Using the Entity Framework, is about reading and writing to Microsoft SQL Server (and other databases) using classic ADO.NET and the object-relational mapping technology known as Entity Framework.

Chapter 9, Querying and Manipulating Data with LINQ, is about Language Integrated Queries (LINQ)—language extensions that add the ability to work with sequences of items, and filter, sort, and project them into different outputs.

Chapter 10, Working with Files, Streams, and Serialization, is about reading and writing to files and streams, text encoding, and serialization.

Chapter 11, Protecting Your Data and Applications, is about protecting your data using encryption and hashing, and checking who is running your application and what they are allowed to do.

Chapter 12, Improving Performance and Scalability with Multitasking, is about allowing multiple actions to be executed at the same time to improve performance, scalability, and user productivity.

Chapter 13, Building Universal Windows Platform Apps Using XAML, is about learning the basics of XAML, which can be used to define the user interface for a graphical app for the Universal Windows Platform (UWP). This app can then run on Windows 10, Windows 10 Mobile, Xbox One, and even HoloLens.

Chapter 14, Building Web Applications and Services Using ASP.NET Core, is about building web applications and services using a modern HTTP architecture on the server side using Microsoft ASP.NET Core 1.0. You will learn about the models, views, and controllers that make up MVC and the Web API.

Chapter 15, Taking C# Cross-Platform, is about introducing you to how you can take C# cross-platform using .NET Core, ASP.NET Core 1.0, Entity Framework Core 1.0, and Visual Studio Code.

Chapter 16, Building a Quiz, is about designing and building a quiz application that helps students learn C#, .NET Core, and related topics.

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

Appendix B, Creating a Virtual Machine for Your Development Environment, shows how to set up a virtual machine in Microsoft Azure for use as a development environment.

What you need for this book

Although you can develop and deploy C# on many platforms, including Mac OS X and Linux, for the best learning experience, you need two pieces of software:

  • Microsoft Windows 10

  • Microsoft Visual Studio Community 2015 with Update 1 (or later)

The best version of Windows to use is Microsoft Windows 10 because you need this version to create Universal Windows Platform apps in Chapter 13, Building Universal Windows Platform Apps Using XAML. Earlier versions of Windows, such as 7 or 8.1, will work for all other chapters. If you don't have a Windows computer, then you can use a virtual machine running Windows in the cloud. Refer to Appendix B, Creating a Virtual Machine for Your Development Environment for instructions.

The best version of Visual Studio to use is Microsoft Visual Studio Community 2015 with Update 1 (or later), which is the version I used while writing this book. In Chapter 15, Taking C# Cross-Platform, I will introduce you to Visual Studio Code, which runs on Windows, Linux, and Mac OS X, and can create cross-platform applications for all these OSes.

Who this book is for

If you have heard that C# is a popular general-purpose, cross-platform programming language used to create everything from business applications, websites, and services to games for mobile devices, Xbox One, and the Windows 10 desktop-to-tablet-to-phone platform, 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 code you will write will be stored in the file named Program.cs."

A block of code is set as follows:

// the best nieces and nephews in the world
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:

// the best nieces and nephews in the world
names[0] = "Kate";
names[1] = "Jack";
names[2] = "Rebecca";
names[3] = "Tom"; 

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

csc my.cs /target:library

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: "In Visual Studio, from the View menu, choose Other Windows, and then C# Interactive."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks 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 e-mail , 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 e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail 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

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 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 , and we will do our best to address the problem.