NET Core 1.0 Modern Cross-Platform Development
Total Page:16
File Type:pdf, Size:1020Kb
C# 6 and .NET Core 1.0 Modern Cross-Platform Development Create powerful cross-platform applications using C# 6, .NET Core 1.0, ASP.NET Core 1.0, and Visual Studio 2015 Mark J. Price BIRMINGHAM - MUMBAI C# 6 and .NET Core 1.0 Copyright © 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: March 2016 Production reference: 1180316 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78528-569-1 www.packtpub.com Credits Author Copy Editor Mark J. Price Pranjali Chury Reviewers Project Coordinator Dustin Heffron Francina Pinto Liviu Ignat Efraim Kyriakidis Proofreader Safis Editing Commissioning Editor Edward Gordon Indexer Tejal Soni Acquisition Editor Kirk D'Costa Production Coordinator Aparna Bhagat Content Development Editor Pooja Mhapsekar Cover Work Aparna Bhagat Technical Editor Vivek Pala About the Author Mark J. Price is a Microsoft Certified Trainer (MCT) and Microsoft Specialist, Programming in C# and Architecting Microsoft Azure Solutions, with more than 20 years of educational and programming experience. Since 1993, Mark has passed more than 90 Microsoft programming exams and specializes in preparing others to pass them too. His students range from professionals with decades of experience to 16-year-old apprentices with no experience at all. He successfully guides all of them by combining educational skills with real-world experience, consulting on and developing systems for enterprises worldwide. Between 2001 and 2003, Mark was employed full-time to write official courseware for Microsoft in Redmond, USA. His team wrote the first training courses for C# and .NET while they were still an early alpha version. While with Microsoft, he delivered "train-the-trainer" classes to get other MCTs up to speed in C# and .NET. In 2010, Mark took a postgraduate certificate in education (PGCE). He taught GCSE and A-level mathematics in two secondary schools in London. Mark holds a Computer Science BSc (Hons) degree from the University of Bristol, UK. I'd like to thank my parents, Pamela and Ian, for raising me to be polite, hardworking, and curious about the world. I'm grateful to my sisters, Emily and Juliet, for loving me despite being their awkward older brother. I'd also like to express my gratitude to my friends and colleagues who inspire me technically and creatively. Lastly, thanks to all the students I have taught over the years for making me strive to be a better teacher, especially Jared. About the Reviewers Dustin Heffron is a software engineer by day and an independent game developer by night. He has over 10 years of experience programming in various languages and 7 years of experience working with C# and .NET. Currently, Dustin works as a software engineer for Johnson Controls Inc. He is also the cofounder and CEO of SunFlake Studios, which was founded in late 2015. He released his first commercial game,Squash Master, in early 2016 and is planning to develop additional games in the coming months and years. Dustin has a long history of reviewing for Packt Publishing, including the book XNA 4.0 Game Development by Example: Beginner's Guide and the video tutorial series XNA 3D Programming by Example. He also coauthored the video tutorial series XNA 3D Toolkit with Larry Louisiana. Liviu Ignat is a full-stack developer and architect, technology geek, and entrepreneur, who has been writing commercial software since 2004. He started with VB6, soon moved to .NET Java, and then continued by moving to front-end web development. He has fun with everything that is a functional language, such as F#, Scala, Swift, JavaScript, and so on. Currently, he is a senior software engineer at AppDirect (http://appdirect.com), Munich, and he is also the CTO of his own startup (http://giftdoodle.com). He has been involved in building distributed backend services, mostly with .NET and complex single-page web apps. He is a big fan of microservices with C#, NodeJS, Scala, and Docker, single-page apps, and native apps with Android and IOS. When he is not coding, Liviu loves snowboarding during the winter, sailing to exotic places during the summer, or just traveling the world. You can find and contact him at http://ignat.email/. Efraim Kyriakidis is a skilled software engineer with over 10 years of experience in developing and delivering software solutions for diverse customers and projects. He's well-versed in all stages of the software development lifecycle. His first acquaintance with computers and programming was a state of the art Commodore 64, back in the '80s as a kid. Since then, he has grown up and received his diploma as Electro technic engineer from Aristotle University Thessaloniki in Greece. Through out his career he has mainly worked with Microsoft technologies, using C# and .NET since .NET 1.0. He currently works for Siemens AG in Germany as a software developer. www.PacktPub.com eBooks, discount offers, and more Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Table of Contents Preface xv Chapter 1: Hello, C#! Welcome, .NET Core! 1 Setting up your development environment 2 Using alternative C# IDEs 2 Using Visual Studio 2015 on Windows 10 3 Installing Microsoft Visual Studio 2015 3 Choosing the default installation 4 Choosing the custom installation 4 Completing the installation 6 Signing in to Visual Studio 6 Choosing development settings 7 Updating extensions and products 8 Using older versions of Visual Studio 10 Understanding .NET Framework, .NET Core, and .NET Native 10 Understanding .NET Framework platform 10 Understanding the Mono project 10 Understanding the .NET Core platform 11 Streamlining .NET 11 The future of .NET 12 Understanding the .NET Native compiler 12 Comparing .NET technologies 13 Writing and compiling code using the Developer Command Prompt 13 Writing code using Notepad 13 Compiling code using the Developer Command Prompt 15 Fixing compiler errors 16 Decompiling code using ILDASM 17 Disassembling compiled assemblies 17 [ i ] Table of Contents Writing and compiling code using Microsoft Visual Studio 2015 19 Writing code using Visual Studio 2015 19 Compiling code using Visual Studio 22 Fixing mistakes with the error list 23 Experimenting with C# Interactive 23 Other useful windows 25 Targeting the .NET Core 26 Understanding the .NET Portability Analyzer 26 Installing the .NET Portability Analyzer 26 Configuring the .NET Portability Analyzer 27 Analyzing a solution 28 .NET Portability and this book 29 Creating new projects for the .NET Core 29 Managing .NET Core development with Command Line Tools 29 Installing the .NET Command Line Tools 30 Creating a .NET Core application using Command Line Tools 30 Creating a .NET Core application using Visual Studio 2015 33 Managing source code with GitHub 36 Using the Team Explorer window 36 Cloning a GitHub repository 37 Managing a GitHub repository 38 Practicing and exploring 39 Exercise 1.1 – test your knowledge 39 Exercise 1.2 – practice managing Visual Studio Windows 39 Exercise 1.3 – practice coding anywhere 40 Exercise 1.4 – explore topics 40 Summary 41 Chapter 2: Speaking C# 43 Understanding C# basics 44 The C# grammar 47 Statements 47 Blocks 48 The C# vocabulary 49 Writing the code 50 Verbs are methods 51 Nouns are types, fields, and variables 52 Counting types and methods 52 Declaring variables 54 Naming variables 54 Storing text 54 Storing numbers 55 Storing whole numbers 55 Storing real numbers 56 [ ii ] Table of Contents Storing Booleans 59 The object type 60 The dynamic type 60 Local variables 60 Inferring the type of a local variable 61 Making a value type nullable 62 Storing multiple values in an array 62 Building console applications 63 Displaying output to the user 63 Getting input from the user 64 Importing a namespace 64 Simplifying the usage of the console in C# 6 65 Reading arguments and working with