Book Image

Learn C# Programming

By : Marius Bancila, Raffaele Rialdi, Ankit Sharma
5 (1)
Book Image

Learn C# Programming

5 (1)
By: Marius Bancila, Raffaele Rialdi, Ankit Sharma

Overview of this book

The C# programming language is often developers’ primary choice for creating a wide range of applications for desktop, cloud, and mobile. In nearly two decades of its existence, C# has evolved from a general-purpose, object-oriented language to a multi-paradigm language with impressive features. This book will take you through C# from the ground up in a step-by-step manner. You'll start with the building blocks of C#, which include basic data types, variables, strings, arrays, operators, control statements, and loops. Once comfortable with the basics, you'll then progress to learning object-oriented programming concepts such as classes and structures, objects, interfaces, and abstraction. Generics, functional programming, dynamic, and asynchronous programming are covered in detail. This book also takes you through regular expressions, reflection, memory management, pattern matching, exceptions, and many other advanced topics. As you advance, you'll explore the .NET Core 3 framework and learn how to use the dotnet command-line interface (CLI), consume NuGet packages, develop for Linux, and migrate apps built with .NET Framework. Finally, you'll understand how to run unit tests with the Microsoft unit testing frameworks available in Visual Studio. By the end of this book, you’ll be well-versed with the essentials of the C# language and be ready to start creating apps with it.
Table of Contents (20 chapters)

What this book covers

Chapter 1, Starting with the Building Blocks of C#, gives an introduction to the language, its history, and its relationship with the Common Language Infrastructure and .NET Framework, as well as providing an introduction to the family of .NET frameworks used today. At the end, you learn about assemblies, how to create a project in Visual Studio, and how to write a Hello World program in C#.

Chapter 2, Data Types and Operators, walks you through the basic elements of the language, including the built-in data types, variables and constants, reference, and value types, nullable types, and array types, as well as type conversions and built-in operators.

Chapter 3, Control Statements and Exceptions, looks in depth at how to write selection statements and loops and briefly at working with exceptions.

Chapter 4, Understanding the Various User-Defined Types, provides information about classes, fields, properties, methods, constructors, how to pass arguments to methods, what access modifiers are, and other aspects related to classes. Toward the end, you will learn about structures and how they compare to classes, as well as enumerations.

Chapter 5, Object-Oriented Programming in C#, continues on the foundation built with the previous chapter and teaches you the core pillars of object-oriented programming and how you achieve them using C# language features such as interfaces, virtual members, method overloading, and others.

Chapter 6, Generics, covers all the aspects of generic programming in C# and teaches you how to write generic types and methods and use constraints for type parameters.

Chapter 7, Collections, provides a walk-through of the generic collections from the .NET base class library that you typically use when writing C# programs. The chapter ends with an overview of the concurrent collections used in multithreading scenarios.

Chapter 8, Advanced Topics, contains a variety of more advanced features, such as delegates and events, tuples, extension methods, pattern matching, and regular expressions.

Chapter 9, Resource Management, explains how the garbage collector works and how you should handle resources deterministically. Also, in this chapter, you learn how to make system or, in general, native API calls with Platform Invocation Services, as well as how to write unsafe code.

Chapter 10, Lambdas, LINQ, and Functional Programming, provides an overview of functional programming concepts and details pertaining to lambda expressions in C#. You learn how to uniformly query various data sources using Language Integrated Query (or LINQ). At the end of the chapter, we cover several typical functional programming concepts: partial function application, currying, closures, monoids, and monads and how they work in C#.

Chapter 11, Reflection and Dynamic Programming, teaches you what reflection services are and how they can be used to write extensible applications, how to dynamically load assemblies and execute code, how to use attributes, and how to use the Dynamic Language Runtime and the dynamic type to interop with dynamic languages.

Chapter 12, Multithreading and Asynchronous Programming, provides an in-depth look at threads, tasks, and synchronization mechanisms and uncovers the details of the async-await pattern for writing asynchronous programs in C#.

Chapter 13, Files, Streams, and Serialization, explains how to work with paths, files, and directories, and how to use streams for reading and writing data from and to a variety of storage options, such as files and memory. In the second part of the chapter, you will learn about data serialization with XML and JSON.

Chapter 14, Error Handling, builds on the concepts concerning exception handling introduced in Chapter 3, Control Statements and Exceptions, and teaches you the inner workings of exceptions and how exception handling differs from error handling. You will learn valuable information about debugging and monitoring as well as best practices for working with exceptions.

Chapter 15, New Features of C# 8, covers in detail all the new language features introduced in C# 8, including nullable reference types, async streams, ranges and indices, pattern matching, and default implementations of interface members.

Chapter 16, C# in Action with .NET Core 3, teaches you about using the .NET CLI for building .NET Core applications, how you can target and develop for Linux, what .NET Standard is and how it can help application design, how to consume NuGet packages, and how you can migrate .NET Framework applications to .NET Core.

Chapter 17, Unit Testing, covers unit testing, the Microsoft tools for unit testing your C# code, how to create unit testing projects using Visual Studio, and how to write unit tests and data-driven unit tests.