Share E-Book

Learning C Through Small Projects (Denis Panjuta, Jafar Jabbarzadeh)(Z-Library)

Author

,

Rating 3.5 AI initial rating

Log in to rate

C/C++/C#
Language English

No Description

Format PDF
Size 22.7 MB
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
Learning C# Through Small Projects Denis Panjuta · Jafar Jabbarzadeh
Page 2
Denis Panjuta Panjutorials GmbH Köln, Germany Jafar Jabbarzadeh Panjutorials GmbH Köln, Germany ISBN 978-3-031-51913-0 ISBN 978-3-031-51914-7 (eBook) https://doi.org/10.1007/978-3-031-51914-7 © The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland AG 2024 This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or informa- tion storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors, and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This Springer imprint is published by the registered company Springer Nature Switzerland AG The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland If disposing of this product, please recycle the paper.
Page 3
v Preface In the ever-evolving world of software development, the ability to adapt, learn, and inno- vate is paramount. C# has emerged as a versatile and powerful language, enabling devel- opers to craft everything from enterprise applications to captivating games. However, the journey to mastering C# can be daunting, especially when faced with dense textbooks and abstract concepts. Learning C# by Small Projects seeks to bridge this gap, offering a hands-on approach to understanding advanced C# concepts through engaging projects and minigames. The rationale behind this book is simple: learning by doing. Instead of wading through pages of theory, you’ll be diving straight into the action, building 11 distinct projects that range from an interactive storytelling program to a responsive Discord chatbot. Each proj- ect is meticulously designed to introduce and reinforce specific C# concepts, ensuring that you not only understand the theory but can also apply it in real-world scenarios. The book is structured to provide a gradual learning curve. The initial chapters lay the foundation, introducing you to the basics of C# programming. As you progress, the proj- ects become more intricate, delving into advanced topics such as asynchronous opera- tions, data integrity, and API integration. By the end of your journey, you’ll have a comprehensive understanding of C# and a portfolio of projects to showcase your skills. As the authors of this book, we, Denis Panjuta and Jafar Jabbarzadeh, have the privilege of sharing our expertise with a combined student base of over 300,000. This book is more than just a guide—it’s a mentor. Our extensive teaching experience is evident in every chapter, ensuring that complex topics are broken down into easily digestible segments. Moreover, our commitment to practical learning ensures that every concept is paired with a hands-on project, reinforcing your understanding and building your confidence. In essence, Learning C# by Small Projects is more than just a book—it’s a journey. A journey that takes you from the foundational concepts of C# to its advanced applications, all while building tangible projects that you can proudly showcase. Whether you’re an aspiring game developer, an enterprise software engineer, or simply a coding enthusiast, this book promises to be an invaluable resource in your C# learning journey.
Page 4
vi So, grab your computer, roll up your sleeves, and let’s dive into the fascinating world of C# development. Your journey to mastering C# starts here. Köln, Germany Denis Panjuta Jafar Jabbarzadeh Preface
Page 5
vii 1 Hello World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 The Technology of Choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 What Can You Do With C#? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2.1 C# for Web Application Development . . . . . . . . . . . . . . . . . . . . . . . 4 1.2.2 C# for Windows Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2.3 C# for Linux and macOS Applications . . . . . . . . . . . . . . . . . . . . . . 5 1.2.4 C# for Mobile App Development . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2.5 C# for Video Game Development . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.3 Getting Started with C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4 Hello World! A Simple Interactive Storytelling App . . . . . . . . . . . . . . . . . . 14 1.4.1 Our Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.4.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.4.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2 C# Data Types and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.1 Variables in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.2 Variable Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.2.1 int (Integral Numeric Types) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.2.2 float (Floating-Point Numeric Types) . . . . . . . . . . . . . . . . . . . . . . . 27 2.2.3 char (Unicode UTF-16 Character) . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.2.4 bool (Boolean Value) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.2.5 The C# Built-in Value and Reference Types (Tables 2.1 and 2.2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.3 Variables Types in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.3.1 Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.3.2 Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.3.3 Array Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.3.4 Value Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 2.3.5 Reference Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Contents
Page 6
viii 2.3.6 Output Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.3.7 Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.4 The Random Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.4.1 Generating a Random Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.5 A Random Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.5.1 Our Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.5.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.5.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3 C# Operators and Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.1 What Are Operators and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.2 Types of Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 3.3 Operator Priority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.3.1 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.3.2 Operator Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3.3.3 Operand Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.4 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3.4.1 The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 3.4.2 The Ternary Operator “?:” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.4.3 The Switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.5 Rock-Paper-Scissors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.5.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3.5.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3.6 Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 3.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4 C# Classes, Methods, and User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4.1 Classes in Object-Oriented C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.1.1 Definition of Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.1.2 Object Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 4.1.3 Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 4.1.4 A Brief Introduction to Test-Driven Development . . . . . . . . . . . . . 79 4.2 Methods in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 4.2.1 Defining a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 4.2.2 Static and Non-static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 4.2.3 Calling a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4.2.4 Method Return Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 4.3 Expanding on the Console Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 4.3.1 The Console Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 4.3.2 Console Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 4.3.3 Console Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Contents
Page 7
ix 4.4 The Convert Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 4.4.1 Definition of the Convert Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 4.4.2 Outcomes to Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 4.4.3 Convert Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 4.5 Guess the Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 4.5.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 4.5.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 4.5.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 4.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 5 C# Collections and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.1 Arrays in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 5.1.1 The Definition of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 5.1.2 Declaring Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 5.1.3 Types of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 5.1.4 Array Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.2 Collections in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5.2.1 The Definition of a Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 5.2.2 System.Collections Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 5.2.3 System.Collections.Concurrent Classes. . . . . . . . . . . . . . . . . . . . . . 126 5.2.4 System.Collections.Generic Classes . . . . . . . . . . . . . . . . . . . . . . . . 127 5.2.5 Collection Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . . . 129 5.2.6 Differences Between Array and Collection . . . . . . . . . . . . . . . . . . . 131 5.2.7 The Shared System.Linq Namespace . . . . . . . . . . . . . . . . . . . . . . . 132 5.3 Iteration Statements in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 5.3.1 Loops and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 5.3.2 Types of Iterators and Loops in C# . . . . . . . . . . . . . . . . . . . . . . . . . 136 5.4 Tic Tac Toe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 5.4.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 5.4.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 5.4.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 5.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 6 C# File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 6.1 The System.IO Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 6.1.1 System.IO Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 6.1.2 System.IO Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 6.1.3 System.IO Structs, Enums, and Delegates . . . . . . . . . . . . . . . . . . . . 174 6.1.4 System.IO for Linux and macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 6.2 String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 6.2.1 The Immutability of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 6.2.2 Verbatim and Quoted Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Contents
Page 8
x 6.2.3 String Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 6.2.4 Format Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 6.2.5 Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 6.3 Guess the Word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 6.3.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 6.3.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 6.3.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 7 C# Async Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 7.1 The Central Processing Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 7.1.1 How Does Asynchrony Work on the CPU . . . . . . . . . . . . . . . . . . . . 200 7.1.2 Asynchrony Versus Multi-threading . . . . . . . . . . . . . . . . . . . . . . . . 200 7.2 Asynchronous Programming in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 7.2.1 What Is Asynchronous Programming in C# . . . . . . . . . . . . . . . . . . 202 7.2.2 Common Use Cases and When to Use. . . . . . . . . . . . . . . . . . . . . . . 202 7.2.3 Overview of the Asynchronous Model . . . . . . . . . . . . . . . . . . . . . . 207 7.3 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 7.3.1 What Is Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 7.3.2 Pattern Matching in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 7.3.3 Types of Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 7.4 Diffuse the Bomb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 7.4.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 7.4.2 Our Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 7.4.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 7.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 8 C# Hashing and Checksum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 8.1 Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 8.1.1 What Is Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 8.1.2 Why Do We Care About Data Integrity . . . . . . . . . . . . . . . . . . . . . . 239 8.1.3 Types of Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 8.2 Checksum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 8.2.1 What Is Checksum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 8.2.2 Checksum Inconsistencies and Why We Shall Not Forget the Simple Things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 8.3 Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 8.3.1 What Is Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 8.3.2 What Is the Difference Between Cryptographic Hashing and Non-cryptographic Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 8.3.3 Types of Cryptographic Hashing Algorithms . . . . . . . . . . . . . . . . . 247 Contents
Page 9
xi 8.4 Checksum Checker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 8.4.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 8.4.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 8.4.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 9 C# Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 9.1 Cybersecurity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 9.1.1 What Is Cybersecurity and Why Should We Care . . . . . . . . . . . . . . 270 9.1.2 What Is the Difference Between Datasecurity and Cybersecurity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 9.1.3 Cybersecurity Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 9.2 Encryption in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 9.2.1 What Is Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 9.2.2 Why Do We Encrypt? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 9.2.3 Difference Between Hashing and Encryption . . . . . . . . . . . . . . . . . 275 9.2.4 Types of Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 9.3 The Process of Encrypting and Decrypting Data . . . . . . . . . . . . . . . . . . . . . 277 9.3.1 Advanced Encryption Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 9.3.2 How Do We Encrypt Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 9.3.3 How Do We Decrypt Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 9.4 The Message Encryptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 9.4.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 9.4.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 9.4.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 10 C# Simple Mail Transfer Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 10.1 Simple Mail Transfer Protocol or SMTP . . . . . . . . . . . . . . . . . . . . . . . . . . 306 10.1.1 How Does SMTP Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 10.1.2 How Do We Use SMTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 10.1.3 SMTP and Receiver Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 10.2 SOLID Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 10.3 JavaScript Object Notation or JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 10.3.1 How Do We Use a JSON File? . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 10.4 A Mass Email Sender App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321 10.4.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 10.4.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 10.4.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Contents
Page 10
xii 11 C# Application Programming Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 11.1 APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 11.1.1 What Is an API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 11.1.2 Why Do We Use APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 11.1.3 How APIs Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 11.1.4 API vs REST vs SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 11.2 The HTTP Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 11.3 Working with APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 11.3.1 What a Simple API Project Would Look Like . . . . . . . . . . . . . . . . 349 11.4 A Discord E-Commerce Bot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 11.4.1 The Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 11.4.2 Our Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 11.4.3 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 11.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 Contents
Page 11
1© The Author(s), under exclusive license to Springer Nature Switzerland AG 2024 D. Panjuta, J. Jabbarzadeh, Learning C# Through Small Projects, https://doi.org/10.1007/978-3-031-51914-7_1 1Hello World! This Chapter Covers • Using the C# programming language • Creating a C# Console Application project • Touring our template of choice • Developing in Visual Studio Community Edition • Building an interactive storytelling app Learning a new skill can often be a daunting and scary task. While certainly not unob- tainable, it feels far to reach and hard to grasp. One must think about finding a better way of accomplishing these goals without turning the journey into a regrettable memory. If we try to search for our favorite learning experiences, we tend to remember moments of accomplishment, self-improvement, and problem-solving. We tend to think of moments when we tried to use our abilities and got closer to mastering them every time. While learning theory is most important, it is practice that makes us experts. This makes us consider why we cannot just turn the learning process into real-life problem-solving exercises. So it is this concept this book focuses on, learning to master a language in a dynamic yet meaningful way. In this chapter, we will go over the steps to build a simple interactive storytelling appli- cation. Similar to Twine or Ren’Py, interactive storytelling applications allow us to write interactive stories with multiple choices and endings. From a simple storytelling app to working efficiently with APIs,1 we will try to learn the C# programming language through a small project in each chapter. To make that happen, we will start by getting to know our technology of choice, its relevance, what 1 Application Programming Interface. A set of programming code that enables data transmission between one software product and another.
Page 12
2 makes this language stand out compared to other options, and where it truly exceeds. Furthermore, we will deep dive into C# development with our first project, learning its similarities with other programming languages we might know and familiarizing our- selves with the entire development process. Let us start this book with our first question: What is C#? 1.1 The Technology of Choice If we come from a C, C++, Java, or Javascript background, we will already be familiarized with the C family of languages. Coming from the C family of languages, the C# object- oriented, type-safe2 programming language is a general-purpose, multi-paradigm3 pro- gramming language. C#’s transition to an open-source model has significantly broadened its scope and appeal, as it enables a collaborative environment where developers can contribute to its evolution and adapt it to emerging technologies and platforms. The modern design of C# facilitates the creation of secure and robust software, and when combined with the .NET Framework, which is also open-source, it empowers developers to build cross-platform, native multi-platform applications. At its core, C# is an object-oriented language enriched with component-oriented features. Its evolution, driven in part by its open-source nature, has incorporated language elements that cater to contemporary design paradigms, making it highly versatile for various applications, including Web apps, games, and mobile apps. This blend of an open-source ecosystem and a continually evolving language design contributes to C#’s position as a staple programming language for many applications, like the following. • Static typing with support for dynamic typing. Variable types are usually declared in advance, and by default, only parameters with the correct data type can be passed. This allows for compile-time checking of type safety, meaning that type errors can be found before the code is run, making the code more reliable. However, since C# 4, C# also supports dynamic typing, which enables variables to be typed at runtime, allowing for more flexibility at the cost of foregoing some compile-time type checking. • Lexically scoped. Variables are only accessible from within the code block in which they are defined, helping to keep the code organized and easy to read. • Imperative and Declarative. It covers both issuing commands to the computer and describing what the program should do, leaving no space for external complications to arise. 2 C# is considered type-safe because it strictly enforces data types and prevents operations that could lead to unpredictable behavior or memory access violations by catching errors at compile time or runtime. 3 A programming paradigm is the classification, style, or way of programming. It is an approach to solve problems by using programming languages. 1 Hello World!
Page 13
3 • Functional. This programming paradigm treats computation as the evaluation of math- ematical functions without changing state and mutable data. It makes it easier to reason and write correct and robust code. Although we will not go any deeper into this within this book, it is an important feature that further solidifies C# in the industry. • Generic. Defines algorithms in terms of types that can be specified later and instanti- ated when needed. It allows for creating data types that are not specific to any one program. This allows for the reusability of code and the ability to create more versatile programs. • Object-oriented. C# supports object-oriented programming (OOP), which primarily revolves around encapsulating data and functions into units called objects. While OOP is often linked with concepts like inheritance and polymorphism, these are not funda- mental to all OOP languages. OOP can be handy for modeling real-world scenarios, but it is not a one-size-fits-all solution. For instance, capturing intricate relationships using OOP can sometimes be cumbersome or lead to design issues. The structure that OOP provides can result in cleaner and more maintainable code, but like any tool, it can be misused. Knowing when and how to use OOP effectively is key. • Language interoperability. The ability of two or more languages to interact with each other. This is important because it allows developers to choose the best language for the task at hand rather than being forced to use a single language for everything. • And component-oriented. The technique of developing software applications by com- bining pre-existing and new components. This means that developers can create librar- ies of code that can be used in multiple applications. This can save a lot of time and money and make it easier to develop large and complex applications. All C# programs run on .NET, using a virtual system called Common Language Runtime (CLR)4 and some class libraries. C# code is compiled into an Intermediate Language5 compatible with the Common Language Interface (CLI), allowing languages and libraries to work together smoothly. .NET provides libraries for various tasks, organized into namespaces (C# groups for related types and members). These libraries include features for file handling, string manipulation, XML parsing, Web frameworks, and Windows Forms controls. We’ll explore namespaces more in a later chapter. C# is one of the most well-known languages. Its community and range of abilities give it a place in almost every project. Nonetheless, there is a question we want to cover to exemplify the current position of C# in the industry and, simultaneously, learn the different paths we, as developers, could go down in our C# careers. 4 Implementation by Microsoft of the Common Language Infrastructure (CLI). 5 Intermediate Language (IL), programming language designed to be used by compilers for the .NET Framework before static or dynamic compilation to machine code. 1.1 The Technology of Choice
Page 14
4 1.2 What Can You Do With C#? Usually, the best way to know if a given technology is the right choice for our use case is to find out if similar projects are done with it. If we are building a Web application, make sure that Web applications were built before with that technology. If we are making games, we need to find games made with that language. Although this book’s projects will stay within a console application, learning a language capable of accomplishing our favorite tasks is, naturally, more often than not the path we will want to take. So to ensure that C# is the right fit, we should learn about some known use cases for C# and its frameworks. Starting with an all-time favorite, Web application development. 1.2.1 C# for Web Application Development C# has been popular for creating Web sites and Web applications, thanks to its ability to help build sites that can change and update in real time. To do this, C# can be used with the .NET Core platform, which is a collection of tools and libraries that makes it easier to develop Web applications. One of the tools within .NET Core is called ASP.NET Core, which is an open-source Web application framework. ASP.NET Core is used for “server-side” Web development. This means it deals with the behind-the-scenes logic that happens on the Web server (a powerful computer that hosts Web sites) before a Web page is sent to your browser. One particular part of ASP.NET Core is called Razor Pages. It makes it simpler to build web pages that primarily stand on their own and do not have overly complicated interac- tion with other pages or parts of the site. It is said to be more “accessible” for developers because it is simpler to understand and work with, especially for those new to Web development. Another interesting tool is Blazor, which allows us to build Web pages where much of the action happens in our web browser and not on the Web server. This means that after the page has loaded, it can continue to update and change without having to reload the page. The Xbox Web site, xbox.com, is a great example of a C#-based Web site. Trustpilot uses C# for web services and app development. Furthermore, StackOverflow, renowned among the programming community for its invaluable resources and discussions, also exemplifies a successful application of these technologies. 1.2.2 C# for Windows Applications This example illustrates a clear application, taking into account that C# was developed both by and for Microsoft. This context streamlines the development process significantly, as every feature needed for Windows application development is seamlessly integrated into the C# language and its encompassing ecosystem. 1 Hello World!
Page 15
5 To serve as examples, we can develop Windows applications through platforms such as WinForms, a platform to write client applications for desktop, laptop, and tablet PCs, aim- ing to simplify this development. Windows presentation foundation, or WPF for short, is a UI framework that creates desktop client applications. Or the MetroFramework, similar to WinForms, is a framework that helps to develop clean applications through a simplified interface. Alternatively, we can also develop Windows applications through a console applica- tion, as done with the projects covered in this book. Examples of applications built with C# include Visual Studio and Microsoft Office. 1.2.3 C# for Linux and macOS Applications Although Linux and macOS applications are not the first things that come to mind when discussing a Microsoft-focused technology, any application can be optimized for Unix- based systems using .NET Core. Its cross-platform development capabilities allow it to develop code usable in most operating systems with little to no required modifications. These applications include, as an example, the previous two mentioned in the preceding subsection. For current-gen ARM processors, like the M1 and M2 chip in the newest Mac comput- ers, Microsoft has an SDK that will allow building and running .NET code on the newest ARM devices (direct link https://dotnet.microsoft.com/en- us/download/dotnet/thank- you/ sdk- 6.0.302- macos- arm64- installer/). Visual Studio can also be used for ARM processors, starting with Visual Studio 2022 for Mac version 17.4 (direct link https://learn.microsoft.com/en- us/visualstudio/releases/2022/ mac- release- notes- preview#17.0.0- pre.5). 1.2.4 C# for Mobile App Development Using C# Xamarin, mobile app development can be possible since Xamarin (direct link https://dotnet.microsoft.com/en- us/apps/xamarin) allows us to wrap native components and libraries into the .NET layer, without the need to rewrite almost any code. As of 2022, a new replacement for Xamarin has arrived with MAUI (direct link https://learn.microsoft. com/en- us/dotnet/maui/what- is- maui?view=net- maui- 7.0), a cross-platform framework for developing native mobile and desktop apps with C# and XAML. Although currently in its early days, it is expected to, once mass adoption takes place, serve as an evolution to Xamarin-based applications. Also, some apps can be directly developed in C# and built for mobile uses, like games. 1.2 What Can You Do With C#?
Page 16
6 We can include apps like the Slack mobile app built on top of Xamarin and various mobile games we will go over next. 1.2.5 C# for Video Game Development If we include indie titles,6 games are mostly made in C# since it is the primary language in game engines like Unity. According to the Unity Gaming Report 2022, the number of games made on the Unity platform increased by 93% this last year (Unity Technologies, 2022) (direct link https://images.response.unity3d.com/Web/Unity/%7B10460b81- b6e7- 4784- a735- e7347afdf06e%7D_Unity- Gaming- Report- 2022.pdf?utm_ source=demand- gen&utm_medium=ceros&utm_campaign=acquisition&utm_ content=2022- gaming- report- ebook&elqTrackId=d813896edf9f48e6af45a569577d8 845&elqaid=4085&elqat=2). It is also an option in engines like CryEngine, Godot, and Stride. Furthermore, it is pos- sible in the popular game engine Unreal Engine, although an initial setup is needed. However, it’s important to mention that while C# is versatile and beginner-friendly, some game developers have concerns regarding its performance characteristics, particu- larly due to its garbage-collected nature. Garbage collection can occasionally cause per- formance hiccups, which can be critical in games that require consistent frame rates. This is why several famous game developers prefer languages with manual memory manage- ment, such as C++. Nevertheless, many successful games, including Cities Skylines, Escape from Tarkov, and mobile games like Genshin Impact, Honkai: Star Rail, and Hearthstone, have been developed using C#. C# and .NET can also be run on a Raspberry Pi identically to any other platform. A .NET app can run as a self-contained app or in framework-dependent deployment modes. With the community behind this programming language and its wide variety of career paths, C# quickly becomes one of the essential programming languages that everyone must learn. So let us start by learning a bit about our context and the environment we will be working in. 1.3 Getting Started with C# Usually the best approach to learning a new language is to first learn everything that sepa- rates it from other languages through studying its context and preferred integrated devel- opment environment (IDE). 6 An “indie title” refers to an independently developed video game, typically created by individuals or small teams without the financial backing or support of a major publisher. 1 Hello World!
Page 17
7 We begin our C# journey with a simple “Hello World” project. Nevertheless, do not worry. The projects will increase in difficulty exponentially, start- ing slow to familiarize us with the context and getting harder over time to challenge the C# proace we are getting along the way. Our first step is to learn about our integrated development environment (IDE), the tool with which we can write and test our code of choice. Luckily, C# counts with a native IDE, Visual Studio. To be specific, Microsoft Visual Studio Community 2022. The community edition, also used with the popular game engine “Unity,” is free to use and can be downloaded through the official Web site (direct link https://visualstudio.microsoft.com/downloads/). Visual Studio Community is a fully featured, extensible, free IDE made for creating modern applications for Android, iOS, and Windows, Web applications, and cloud services, so chances are that we might have familiarized ourselves with it before. After setting up and starting Visual Studio, a process discussed in the appendix in case we need it, we will find ourselves at the project creation window. Here is where we can begin our first traditional project. We want to write some code that displays a “Hello World!” message, such as with HTML tags, labels, pop-ups, or other methods that we typically find in other programming languages and frameworks. In our case, we will display it using our Console, so a simple message will show when running the program. Simple enough, but remember that our primary goal is to familiarize ourselves with the process. We will have to tackle asynchronous programming and application programming interfaces soon enough. In the Microsoft Visual Studio Launcher, we will find a few options. One of them is to create a new project. That is what we want to do. Continue by clicking on “Create a new project” (Fig. 1.1). Fig. 1.1 We are starting by selecting the “Create a new project” option In the next screen, select the template we will use for this project, a Console App. We will also find the last template used in “Recent project templates” if we previously created a project on the left side. Since we are starting this book, assuming we have not yet created a project, we will have to select it from the list on the right. 1.3 Getting Started with C#
Page 18
8 We will find a list of templates currently installed on our device. This list varies depend- ing on the packages we choose in the installation process. Usually, the first template is the one we want here, specifically the C# Console App. We do need to make sure to select the correct one since there are several console app tem- plates. We will only need the plain Console App (Fig. 1.2). This template will give us a preconfigured project to create a command-line application on .NET Core on Windows, Linux, and macOS devices. After continuing by pressing the Next button, we will be prompted with a window that asks us for a project name, location, and Solution Name. We want to provide a descriptive name, like “Hello World,” and place it in our preferred directory. Also, there will be a checkbox to place the solution. A solution is a container for one or more projects. It allows us to manage dependencies and target different platforms with our code. We can see a solution as a box containing several projects, and since they are together, they can share code among them, simplifying development. This is generally done with, for example, Web development. Although we can create a single solution for each of our projects, this time, for simplic- ity and to keep the projects separate, we will create a new solution for each one. We can change the solution location individually by unchecking, but that will not be needed here; therefore, we will keep it checked. In case it does not default to a checked state, just make sure to check it. Then continue by pressing the Next button again (Fig. 1.3). Fig. 1.2 We will mainly use the Console App project template for the projects in this book Fig. 1.3 Name the project correctly and check the checkbox 1 Hello World!
Page 19
9 In the next window, we can decide which framework to use for the project. This option is the target framework we will be working on. Since we do not want to start a project entirely from scratch, we always want to start with something. In this exam- ple, we are building a .NET application, meaning we need to use a .NET Framework. The available versions depend on the ones we installed before. In our case, we can find .NET 6 and .NET 5. We will be working with .NET 6 in this project, so just select “.NET 6.0 (Long term support)” from the dropdown menu and press “Create” to finally start up our first project. In newer versions of Visual Studio we can also find a checkbox for “Do not use top- level statements.” Although we typically will leave that unchecked, it will not be relevant for the correct following of this book, as we will provide the base code we will start with in every chapter. However, in short, when we check that checkbox, we get the traditional structure with a “Program” class and Main() method as a result. If not, we will get our result we will see shortly (Fig. 1.4). Fig. 1.4 We will be using .NET version 6.0 for our projects A ready-to-go console app setup should greet us with the project configured to use .NET 6. Now, what do we want to achieve, and how do we plan to do it? Our application will be a simple program to print a “Hello, World!” message in our Console. This goal should be, especially now with .NET 6, really straightforward. Looking at the current project, we can see that the chosen template has already gener- ated a Program.cs file, which includes a line of code with the exact message we want to display. But does that mean we are already done with our project? Is that it? Technically yes! However, we are not here just for that. We are here to understand how all of this works (Fig. 1.5). Fig. 1.5 We can see the base template given by the chosen console app template 1.3 Getting Started with C#
Page 20
10 First, let us investigate what the project template includes to understand our working environment better. On the right-hand side, we can see the Solution Explorer. That window will show our project file structure, including the Program.cs file we just discovered. If we right-click on the project name and select “Open Folder in the File Explorer,” we can see the entire folder structure that Visual Studio generated. The .sln file represents the solution that Visual Studio uses. That is the solution we selected a location for in the project configuration and is what we will be opening with Visual Studio in the future. Alongside the .sln file, we can find all the project contents. From the Program.cs file we saw open earlier to all the future files and folders we will be creating in future projects. This folder structure will change depending on the template, especially with more feature- rich templates. The number of folders and the depth of the layers can increase (Fig. 1.6). Fig. 1.6 The project structure within the Windows file explorer Double-click on “Program.cs” to open directly in our IDE to get back to our project. We can open these files with other text editors we might have installed, but when installing Visual Studio, it sets itself as the default tool to open files with the .cs extension type. The advantage of using Visual Studio instead of a standard text editor is mainly in the ease of use but it is not limited to that. One significant advantage is Visual Studio’s advanced error detection, IntelliSense. If we were to type out incorrect code, a regular text editor would not detect these issues, while our IDE notifies us and gives us possible solutions to that error. This advantage significantly eases the building and debugging of our applications. Since .NET 6, the Visual Studio template has been simplified to the point where the files we work with do not have anything other than our custom code, with no need to add any boilerplate code. In the past, we usually found namespace and class declarations. Now, we only find the code that we want to run. That does not mean that namespace and class declarations are not needed anymore, but the structure changed such that we do not need to repeat that code in every program we create. .NET 6 brought that requirement down by introducing top-level statements. This lan- guage feature allows us to implicitly set an entry point by writing statements outside a type declaration. By doing this, we no longer need to explicitly set a Main() method. Technically, 1 Hello World!
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay

Every 10 yuan is 1 year of VIP; the remainder counts as months (5 yuan is 5 months, 100 yuan is 10 years). Signed-in users are enabled immediately and both you and the admin get an email. Guests receive an ID and are sent to register.

← Back to List