NET Core 3 and Unit Testing
Total Page:16
File Type:pdf, Size:1020Kb
Learn C# Programming A guide to building a solid foundation in C# language for writing efcient programs Marius Bancila Rafaele Rialdi Ankit Sharma BIRMINGHAM—MUMBAI Learn C# Programming Copyright © 2020 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 efort 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 authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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. Commissioning Editor: Richa Tripathi Acquisition Editor: Alok Dhuri Senior Editor: Storm Mann Content Development Editor: Ruvika Rao Technical Editor: Pradeep Sahu Copy Editor: Safs Editing Language Support Editor: Safs Editing Project Coordinator: Francy Puthiry Proofreader: Safs Editing Indexer: Pratik Shirodkar Production Designer: Jyoti Chauhan First published: April 2020 Production reference: 1280420 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78980-586-4 www.packt.com To my smart boys, Cristian and Bogdan, who love learning new things every day. – Marius To my mom and dad, who ignited my passion with a Sinclair ZX81 when I was a child, and to my beautiful wife, Valeria, who has always supported me in my adventures. I love you! – Rafaele To my mother, Vibha Sharma, for everything she did for me. – Ankit Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals • Improve your learning with Skill Plans built especially for you • Get a free eBook or video every month • Fully searchable for easy access to vital information • Copy and paste, print, and bookmark content Did you know that Packt ofers eBook versions of every book published, with PDF and ePub fles available? You can upgrade to the eBook version at packt.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.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and ofers on Packt books and eBooks. Foreword As a developer of the 2020s, it's hard to believe that there was a time in which the choice of programming language was crucial for the success of a sofware project, with each programming language coming with its own set of libraries defning the range of possible actions. Data access, for example, was a no-brainer in Visual Basic but hard to do in C++, for example. By the same token, calling into low-level Windows API functions was easy in C but sometimes impossible in Visual Basic. Ten came the .NET platform made of two distinct pillars. One pillar was an object- oriented framework defning a wide range of predefned behaviors. Te other pillar was a language runtime able to support a variety of programming languages, including Visual Basic and the new C#. Over the years, C# has then become the primary language for the whole .NET platform. So, what does it mean to learn C# today, two decades afer .NET 1.0? Learning the sole syntax and semantics of a programming language makes little to no sense these days, whether it is C#, Java, or Python. A programming language is a largely interchangeable tool with limited real programming power without the backbone of a solid framework. Subsequently, learning the plain syntax of a programming language is a small fraction of the job and a book that intends to cover any programming language has to go well beyond data types, operators, and control statements. Tis is precisely what this book does and in addition, in it, you will also explore the authors' well-known obsession through clear and precise topics. Tis book is not simply a technical guide about using the C# language. It covers statements, data types, classes, generics, concurrency, and even functional and dynamic programming, plus the array of new features in the latest C# language. However, if you are only armed with these tools, you won't go too far even with building toy applications. It is also important for you to know about the foundational aspects of the .NET platform, such as refection, collections, regular expressions, fles, streams, serialization, and LINQ. Anything else? You bet! Error handling, exceptions, garbage collection, and memory management also have a reserved chapter, before ending with an overview of C#8 in the context of .NET Core 3 and unit testing. In spite of the title, this is not simply the umpteenth book on a popular programming language. It is a book about how to use a popular programming language to its fullest for building .NET applications on a solid foundation. Dino Esposito Digital Strategist, Youbiquitous.net Contributors About the authors Marius Bancila is a sofware engineer with almost two decades of experience in developing solutions for the industrial and fnancial sectors. He is the author of Modern C++ Programming Cookbook and Te Modern C++ Challenge. He works as a sofware architect and is focused on Microsof technologies, mainly developing desktop applications with C++ and C#, but not solely. He is passionate about sharing his technical expertise with others and, for that reason, he has been recognized as a Microsof MVP for C++ and later developer technologies since 2006. I would like to thank everybody that made this book possible. To Rafaele and Ankit for laboring on this project together and making it a great book. To Omprakash for his useful comments. To Ruvika for all her support and patience during the project. To Alok, Storm, and all the other people at Packt that helped turn this book from an idea to reality. Rafaele Rialdi is a senior sofware architect working as a consultant, speaker, and trainer. Since 2003, he is a Microsof MVP in the Developer Security category. His passion for the community leads him to be a member of the board of UGIdotNET, president of DotNetLiguria, and co-founder of the Italian C++ user group. Currently, he is working as an architect and developer on the backend of an enterprise project with a specifc focus on code generation, and working on cross-platform mobile and IoT development in both C# and C++. You can fnd him on Twitter with the handle @raffaeler. A big thank you goes to my fellow co-authors, Marius and Ankit, for their great work; Omprakash for his valuable reviews; the entire Packt team, who did a great job in letting me work with peace of mind; and, last but not least, Ruvika for her great patience in coordinating with all of us. Ankit Sharma is a sofware engineer currently working as Senior Member Technical with ADP in Hyderabad, India. He has over six years of extensive experience with Microsof technologies, including C#, ASP.NET, and SQL Server, and UI technologies such as jQuery, Angular, and Blazor. Ankit is a technical author and speaker and loves to contribute to the open source community. He writes articles for multiple platforms, including c-sharpcorner, Dzone, Medium, and freeCodeCamp. For his dedicated contribution to the developer's community, he has been recognized as c-sharpcorner MVP, Dzone MVB, and a top contributor in the technology category on Medium. He is also the author of the frst ever book on Blazor – Blazor Quick Start Guide. You can tweet him at @ankitsharma_007. I would like to thank my mother for her continuous support throughout the process of writing this book. I would also like to thank my co-authors, Marius and Rafaele, for their constructive feedback throughout the writing process, which was crucial in enhancing the quality of the content. About the reviewer Omprakash Pandey is a seasoned sofware technology professional associated with Synergetics. He has provided innovative solutions on C, C++, Java (Core, Intermediate, and Advanced), .NET (Basic), ADO.NET, WCF, WF, ASP.NET MVC, Microsof 365, Microsof Azure, infrastructure management for portal development and deployment, and services development. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors. packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specifc hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Starting with the Building Blocks of C# The history of C# 18 Xamarin 32 Understanding the CLI 20 Assemblies in .NET 33 Common Type System (CTS) 22 Global Assembly Cache (GAC) 33 Common Language Specifcation (CLS) 22 Runtime package store