Learn C# Programming

Book description

Get started with C# and strengthen your knowledge of core programming concepts such as procedural, object-oriented, generic, functional, and asynchronous programming along with the latest features of C# 8

Key Features

  • Learn the fundamentals of C# with the help of easy-to-follow examples and explanations
  • Leverage the latest features of C# 8, including nullable reference types, pattern matching enhancements, and asynchronous streams
  • Explore object-oriented programming, functional programming, and multithreading concepts

Book Description

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.

What you will learn

  • Get to grips with all the new features of C# 8
  • Discover how to use attributes and reflection to build extendable applications
  • Utilize LINQ to uniformly query various sources of data
  • Use files and streams and serialize data to JSON and XML
  • Write asynchronous code with the async-await pattern
  • Employ .NET Core tools to create, compile, and publish your applications
  • Create unit tests with Visual Studio and the Microsoft unit testing frameworks

Who this book is for

If you have little experience in coding or C# and want to learn the essentials of C# programming to develop powerful programming techniques, this book is for you. It will also help aspiring programmers to write scripts or programs to accomplish specific tasks.

Table of contents

  1. Learn C# Programming
  2. Why subscribe?
  3. Foreword
  4. Contributors
  5. About the authors
  6. About the reviewer
  7. Packt is searching for authors like you
  8. Preface
    1. Who this book is for?
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Code in Action
    6. Download the color images
    7. Conventions used
    8. Get in touch
    9. Reviews
  9. Chapter 1: Starting with the Building Blocks of C#
    1. The history of C#
    2. Understanding the CLI
      1. Common Type System (CTS)
      2. Common Language Specification (CLS)
      3. Common Intermediate Language (CIL)
      4. Virtual Execution System (VES)
    3. The .NET family of frameworks
      1. .NET Framework
      2. .NET Core
      3. Xamarin
    4. Assemblies in .NET
      1. Global Assembly Cache (GAC)
      2. Runtime package store
    5. Understanding the basic structure of a C# program
    6. Summary
    7. Test what you learned
  10. Chapter 2: Data Types and Operators
    1. Basic data types
      1. The integral types
      2. The floating-point types
      3. The decimal type
      4. The char type
      5. The bool type
      6. The string type
      7. The object type
    2. Variables
      1. Naming convention
      2. Implicity-typed variables
      3. Understanding the scope and lifetime of variables
    3. Understanding constants
    4. Reference types and value types
      1. Value types
      2. Reference types
      3. Boxing and unboxing
    5. Nullable types
    6. Arrays
      1. One-dimensional arrays
      2. Multi-dimensional arrays
      3. Jagged arrays
    7. Type conversion
      1. Implicit type conversion
      2. Explicit type conversion
      3. User-defined type conversions
      4. Conversions with helper classes
    8. Operators
      1. Arithmetic operators
      2. Relational operators
      3. Logical operators
      4. Bitwise and shift operators
      5. Assignment operators
      6. Other operators
    9. Summary
    10. Test what you learned
  11. Chapter 3: Control Statements and Exceptions
    1. Understanding control statements
      1. Selection statements
      2. Iteration statements
      3. The jump statements
    2. Exception handling
    3. Summary
    4. Test what you learned
  12. Chapter 4: Understanding the Various User-Defined Types
    1. Classes and objects
      1. Fields
      2. Methods
      3. Constructors
      4. Properties
      5. Indexers
      6. The this keyword
      7. The static keyword
      8. The ref, in, and out parameters
      9. Methods with a variable number of arguments
      10. Named and optional arguments
      11. Access modifiers
      12. Partial classes
    2. Structures
    3. Enumerations
    4. Namespaces
    5. Summary
    6. Test what you learned
  13. Chapter 5: Object-Oriented Programming in C#
    1. Understanding OOP
    2. Abstraction
    3. Encapsulation
    4. Inheritance
      1. Virtual members
      2. Abstract classes and members
      3. Sealed classes and members
      4. Hiding base class members
      5. Interfaces
    5. Polymorphism
      1. Method overloading
      2. Operator overloading
    6. SOLID principles
    7. Summary
    8. Test what you learned
    9. Further Reading
  14. Chapter 6: Generics
    1. Understanding generics
    2. Generic types
      1. Generic classes
      2. Inheritance with generic classes
      3. Generic interfaces
      4. Variant generic interfaces
      5. Generic structures
    3. Generic methods
    4. Type parameter constraints
    5. Summary
    6. Test what you learned
  15. Chapter 7: Collections
    1. Introducing the System.Collections.Generic namespace
    2. The List<T> collection
    3. The Stack<T> collection
    4. The Queue<T> collection
    5. The LinkedList<T> collection
    6. The Dictionary<TKey, TValue> collection
    7. The HashSet<T> collection
      1. Choosing the right collection type
    8. Using thread-safe collections
      1. IProducerConsumerCollection<T>
      2. BlockingCollection<T>
      3. ConcurrentQueue<T>
      4. ConcurrentStack<T>
      5. ConcurrentBag<T>
      6. ConcurrentDictionary<TKey, TValue>
      7. Choosing the right concurrent collection type
    9. Summary
    10. Test what you learned
  16. Chapter 8: Advanced Topics
    1. Delegates and events
      1. Delegates
      2. Events
    2. Anonymous types
    3. Tuples
      1. The Tuple class
      2. Value tuples
    4. Pattern matching
      1. The is expression
      2. The switch expression
    5. Regular expressions
      1. Overview
      2. Matching input text
      3. Finding substrings
      4. Replacing parts of a text
    6. Extension methods
    7. Summary
    8. Test what you learned
  17. Chapter 9: Resource Management
    1. Garbage collection
    2. Finalizers
    3. The IDisposable interface
    4. The using statement
    5. Platform invoke
    6. Unsafe code
    7. Summary
    8. Test what you learned
    9. Further reading
  18. Chapter 10: Lambdas, LINQ, and Functional Programming
    1. Functional programming
    2. Functions as first-class citizens
    3. Lambda expressions
    4. LINQ
      1. Standard query operators
      2. Query syntax
    5. More functional programming concepts
      1. Partial function application
      2. Currying
      3. Closures
      4. Monoids
      5. Monads
    6. Summary
    7. Test what you learned
  19. Chapter 11: Reflection and Dynamic Programming
    1. Understanding reflection
    2. Dynamically loading assemblies
    3. Understanding late binding
    4. Using the dynamic type
    5. Attributes
      1. System attributes
      2. User-defined attributes
      3. How to use attributes?
      4. Attribute targets
      5. Assembly attributes
      6. Attributes in reflection
    6. Summary
    7. Test what you learned
  20. Chapter 12: Multithreading and Asynchronous Programming
    1. What is a thread?
    2. Creating threads in .NET
      1. Using the ThreadPool class
    3. Understanding synchronization primitives
    4. The task paradigm
      1. Synchronous implementations of asynchronous methods
      2. Occasionally asynchronous methods
      3. Breaking the task chain – blocking the thread
      4. Manually creating a task
      5. Long-running tasks
      6. Breaking the task chain – fire and forget
      7. Task and exceptions
      8. Canceling a task
      9. Monitoring the progress of a task
      10. Parallelizing tasks
      11. Signaling tasks with the TaskCompletionSource object
      12. Synchronization context
    5. Summary
    6. Test what you learned
    7. Further reading
  21. Chapter 13: Files, Streams, and Serialization
    1. Overview of the System.IO namespace
    2. Working with paths
    3. Working with files and directories
    4. Working with streams
      1. Overview of the stream classes
      2. Working with file streams
      3. Working with memory streams
    5. Serializing and deserializing XML
    6. Serializing and deserializing JSON
      1. Using Json.NET
      2. Using System.Text.Json
    7. Summary
    8. Test what you learned
  22. Chapter 14: Error Handling
    1. Errors
    2. Exceptions
      1. Catching exceptions
      2. The exception objects
      3. Conditional catch
      4. Throwing exceptions
      5. Creating a custom exception type
      6. Rethrowing an exception
      7. The finally block
    3. Debugging and monitoring exceptions
      1. Debugging second-chance exceptions
      2. Debugging first-chance exceptions
      3. AppDomain exception events
      4. Logging exceptions
    4. Summary
    5. Test what you learned
    6. Further reading
  23. Chapter 15: New Features of C# 8
    1. Nullable reference types
      1. Enabling the nullable reference type feature
      2. Working with nullable reference types
      3. Migrating existing code to nullable reference types
    2. Default implementation of interface members
      1. Interface versioning
      2. Interface reabstraction
      3. Interfaces as traits
    3. Ranges and indices
    4. Pattern matching
      1. Recursive pattern matching
    5. The using declaration
    6. Asynchronous Dispose
    7. Disposable patterns in structs and ref structs
    8. Asynchronous streams
    9. Readonly struct members
    10. Null coalescing assignment
    11. Static local functions
    12. Better interpolated verbatim strings
    13. Using stackalloc in nested expressions
    14. Unmanaged constructed types
    15. Summary
    16. Test what you learned
    17. Further reading
  24. Chapter 16: C# in Action with .NET Core 3
    1. Using the .NET command-line interface (CLI)
    2. Developing on Linux distributions
      1. Preparing the development box
      2. Writing cross-platform aware code
    3. What .NET Standard is and how can it help the application design
      1. Creating a .NET Standard library
    4. Consuming NuGet packages
      1. Adding packages to a project
    5. Migrating from .NET Framework to .NET Core
      1. Analyzing your architecture
      2. Preparing the migration process
      3. Migrating the libraries
      4. Migrating the tests
      5. Migrating the desktop projects
      6. Migrating ASP.NET projects
      7. Summing up the migration steps
    6. Publishing an application
      1. Publishing as an FDD
      2. Publishing as an SCD
      3. Understanding other publishing options
    7. Summary
    8. Test what you learned
    9. Further reading
  25. Chapter 17: Unit Testing
    1. What is unit testing?
    2. What are Microsoft tools for unit testing?
    3. Creating a C# unit testing project
    4. Writing unit tests
      1. Analyzing code coverage
      2. The anatomy of a test
    5. Writing data-driven unit tests
      1. Data from attributes
      2. Dynamic data
      3. Data from external sources
    6. Summary
    7. Test what you learned
  26. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
  27. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learn C# Programming
  • Author(s): Marius Bancila, Raffaele Rialdi, Ankit Sharma
  • Release date: April 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781789805864