From Your First Line of Code to Building Professional-Grade Software
Introduction
Why C#?
What Is .NET?
How This Book Is Structured
How to Use This Book
What You Will Be Able to Do After This Book
Chapter 1: Getting Started with Programming and C#
What Is Programming and Why C#?
The .NET Ecosystem Explained
Installing Your Development Environment
Understanding How Code Becomes a Program
Writing Your First C# Program
Reading and Understanding C# Syntax
Modern C# Program Structure
Compiling and Running Your Code
Comments in C# Code
Your First Interactive Program
Chapter Summary
Chapter 2: Variables, Data Types and Operators
What Are Variables and Why Do We Need Them?
Naming Variables: Rules and Conventions
Primitive Data Types in C#
Numeric Types and Precision Considerations
- Integer Types
- Floating-Point Types
Strings and Text Manipulation
- Declaring and Initializing Strings
- Escape Sequences
- String Immutability
- Common String Operations
Boolean Values and Logical Thinking
Type Conversion and Casting
- Implicit Conversions
- Explicit Conversions (Casting)
- Parse and TryParse Methods
Operators: Arithmetic, Comparison and Logical
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Operator Precedence and Readability
A Practical Example: Temperature Converter
Chapter Summary
Chapter 3: Control Flow
Conditional Logic with If and Else
- Basic If Statement
- If-Else Statement
- If-Else If-Else Chains
- Single-Line If Statements
- Combining Conditions
Switch Statements and Pattern Matching Basics
- Traditional Switch Statement
- Switch Expressions (C# 8+)
- Switch with Ranges and Conditions
- Switch with Different Types
While Loops for Unknown Iteration Counts
- Basic While Loop
- While Loop with User Input
- Do-While Loop
For Loops for Known Iteration Counts
- Basic For Loop
- Practical For Loop Examples
Foreach Loops for Collections
- Basic Foreach Loop
- Foreach with Index
Loop Control: Break, Continue and Labels
- Break: Exit the Loop
- Continue: Skip to Next Iteration
- Labeled Break and Continue: Controlling Outer Loops
Nested Control Structures and Readability
- Problematic Deep Nesting
- Improved with Early Returns and Extraction
Choosing the Right Control Structure
A Practical Example: Number Guessing Game
Chapter Summary
Chapter 4: Methods and Scope
Why Methods Matter for Organized Code
Defining and Calling Methods
- Basic Method Definition and Call
- Methods with Return Values
- Multiple Return Statements
Parameters and Arguments
- Single and Multiple Parameters
- Default Parameters
- Named Arguments
Return Values and Void Methods
- Void Methods
- Methods Returning Values
Parameter Passing: Value vs Reference
- Pass by Value (Default Behavior)
- Pass by Reference with ref
- Pass by Reference with out
- Reference Types and Pass by Value
Method Overloading
Variable Scope and Lifetime
- Scope Levels
- Lifetime
Best Practices for Writing Clean Methods
- 1. Single Responsibility
- 2. Meaningful Names
- 3. Keep Methods Short
- 4. Limit Parameters
- 5. Avoid Side Effects When Possible
- 6. Use Guard Clauses Instead of Deep Nesting
A Practical Example: Text Statistics Analyzer
Chapter Summary
Chapter 5: Object-Oriented Programming Fundamentals
What Is Object-Oriented Programming?
Classes as Blueprints for Objects
- Defining Your First Class
- Class Structure Anatomy
Fields and Data Encapsulation
- Access Modifiers
- Why Encapsulation Matters
Properties: Controlled Access to Data
- Auto-Implemented Properties
- Properties with Validation
- Expression-Bodied Properties (C# 6+)
- Init-Only Properties (C# 9+)
Constructors and Object Initialization
- Basic Constructor
- Constructors with Parameters
- Multiple Constructors (Overloading)
- Object Initializers
Methods as Object Behavior
- Instance Methods
- The this Keyword
Static Members vs Instance Members
- Instance Members
- Static Members
- Static Methods
- When to Use Static vs Instance
Practical Example: Building a Bank Account System
Chapter Summary
Chapter 6: Inheritance and Polymorphism
The Concept of Inheritance
Base Classes and Derived Classes
Method Overriding with Virtual and Override
- The virtual Keyword
- Why Use Virtual Methods?
The Base Keyword and Calling Parent Behavior
- Calling Base Constructors
- Calling Base Methods
- Accessing Base Properties
Polymorphism: Treating Objects Uniformly
- Basic Polymorphism
- Polymorphism in Methods
Sealed Classes and Methods
- Sealed Classes
- Sealed Methods
- Closed Hierarchies (C# 15+)
Object as the Universal Base Type
- Overriding ToString()
- Boxing and Unboxing
Practical Example: A Shape Hierarchy with Area Calculations
Chapter Summary
Chapter 7: Interfaces, Abstraction and Design Principles
What Are Interfaces and Why Use Them?
- Why Use Interfaces?
Implementing Multiple Interfaces
Abstract Classes vs Interfaces
- Key Differences
- When to Use Each
- Example Comparing Both Approaches
Dependency Inversion Principle
- Problem Without DIP
- Solution With DIP
The SOLID Principles Explained
- S - Single Responsibility Principle (SRP)
- O - Open/Closed Principle (OCP)
- L - Liskov Substitution Principle (LSP)
- I - Interface Segregation Principle (ISP)
- D - Dependency Inversion Principle (DIP)
Encapsulation Revisited: Information Hiding
Practical Example: A Payment Processing System with Strategy Pattern
Chapter Summary
Chapter 8: Collections and Generics
Arrays: Fixed-Size Ordered Collections
- Creating and Using Arrays
- Array Initialization Shorthand
- Collection Expressions (C# 12+)
- Array Limitations
The List Class for Dynamic Collections
- Creating and Using Lists
- Common List Operations
- Performance Considerations
Dictionaries for Key-Value Lookups
- Creating and Using Dictionaries
- Practical Dictionary Example
- Dictionary Performance
Sets for Unique Elements
Queues and Stacks for Ordered Processing
- Queue (FIFO: First In, First Out)
- Stack (LIFO: Last In, First Out)
Understanding Generics and Type Parameters
- Why Generics Matter
- Creating Generic Classes
- Generic Methods
- Multiple Type Parameters
Generic Constraints
- Common Constraint Types
Choosing the Right Collection Type
A Practical Example: Contact Management System
Chapter Summary
Chapter 9: LINQ and Querying Data
What Is LINQ and Why It Matters
Method Syntax vs Query Syntax
- Method Syntax (Fluent API)
- Query Syntax
- Mixing Syntaxes
- When to Use Which
Filtering with Where
Transforming Data with Select
Ordering and Sorting Results
Grouping and Aggregating Data
- Grouping
- Aggregation Methods
Joining Multiple Data Sources
- Outer Joins: LeftJoin, RightJoin and FullJoin
Deferred Execution and Performance
- Understanding Deferred Execution
- Immediate Execution
- Performance Implications
A Practical Example: Sales Analytics Dashboard
Chapter Summary
Chapter 10: Exception Handling and File I/O
What Are Exceptions and Why Handle Them?
- The Exception Hierarchy
Try-Catch-Finally Blocks
- Basic Try-Catch
- Multiple Catch Blocks
- Try-Finally
- Try-Catch-Finally Combined
Creating Custom Exception Types
Best Practices for Exception Handling
- 1. Catch Specific Exceptions
- 2. Use TryParse Instead of Parse When Appropriate
- 3. Preserve Stack Traces When Rethrowing
- 4. Use Using Statements for Resource Cleanup
- 5. Document Exceptions
Reading from Files
- Simple File Reading
- Reading Large Files with StreamReader
- Reading with Encoding
Writing to Files
- Simple File Writing
- Writing with StreamWriter
- Appending to Files
Working with Directories and Paths
- Path Operations
- Directory Operations
Practical Example: A Configuration File Manager
Chapter Summary
Chapter 11: Delegates, Events and Callbacks
What Are Delegates?
- Defining a Delegate
Defining and Using Delegates
- Callback Pattern
- Built-in Delegate Types
Anonymous Methods and Lambda Expressions
- Lambda Expressions
- Lambda Capture (Closures)
- Static Lambdas
Events: The Publish-Subscribe Pattern
- Defining and Raising Events
- Standard Event Pattern
Event Handlers and Conventions
- Common EventHandler Types
- Multiple Subscribers
Combining LINQ with Delegates
Practical Example: A Stock Price Notification System
Chapter Summary
Chapter 12: Asynchronous Programming and Multithreading
Why Asynchronous Programming Matters
- The Problem with Blocking Code
- The Solution: Async/Await
The Await Keyword and Async Methods
- Basic Async Method
- Async Void vs Task
- Chaining Async Operations
The Task Type and Task Scheduler
- What Is a Task?
- Running Operations in Parallel
- WhenAny vs WhenAll
Handling Errors in Async Code
- Async Exception Best Practices
Threading Fundamentals
- The Thread Pool
- Task.Run for CPU-Bound Work
- Parallel.For and Parallel.ForEach
Thread Synchronization and Locking
- Race Conditions
- Using lock
- The Lock Type (C# 13+)
- Interlocked Operations
- SemaphoreSlim for Limited Concurrency
Concurrent Collections
Practical Example: A Web Scraper with Parallel Processing
Chapter Summary
Chapter 13: Advanced Language Features
Nullable Reference Types for Safer Code
- Enabling Nullable Reference Types
- How It Works
- NRTs in Classes and Methods
- NRT Best Practices
Records for Immutable Data Models
- Record Classes
- Record with Custom Behavior
- Record Structs vs Record Classes
Pattern Matching Deep Dive
- Type Patterns
- Property Patterns
- Positional Patterns
- List Patterns (C# 12)
Properties with Expressions and Init Only Setters
- Expression-Bodied Members
- Init-Only Setters
Spans and Memory-Efficient Operations
- What Is Span?
- Span for String Operations
- Memory for Async Scenarios
Garbage Collection and Memory Management
- Stack vs Heap: Where Data Lives
- How the Garbage Collector Works
- The Collection Process
- Roots and Reachability
- Memory Leaks in Managed Code
- Finalizers and the Dispose Pattern
- Reducing GC Pressure
- Diagnosing Memory Issues
Source Generators Overview
Top-Level Statements and Modern Program Structure
Practical Example: A High-Performance Data Parser
Chapter Summary
Chapter 14: Modern .NET Development Ecosystem
The .NET Command Line Interface
- Essential Commands
- Creating Projects
- Building and Running
- Publishing for Deployment
NuGet Package Management
- Adding Packages
- Common Useful Packages
- Security Auditing
- Best Practices
Dependency Injection Fundamentals
- How DI Works
- Service Lifetimes
- Benefits of DI
Reflection and Runtime Inspection
- Basic Reflection
- Performance Considerations
Attributes for Metadata
- Built-in Attributes
- Custom Attributes
Unit Testing with xUnit and Moq
- Setting Up Tests
- Writing Tests
Debugging Techniques and Tools
- Using Breakpoints
- Conditional Breakpoints
- Watch Windows
- Logging for Production
Security Best Practices in C#
- Input Validation
- Secure Password Handling
- SQL Injection Prevention
- Secure Configuration
- Managing Secrets Securely
- HTTPS and Certificate Handling
- Web Security: CSRF, XSS and Common Vulnerabilities
Networking with HttpClient
- HTTP Fundamentals
- HttpClient Best Practices
- Making HTTP Requests
- Handling Errors and Timeouts
- Basic Socket Programming Concepts
Serialization: Converting Objects to Data
- JSON Serialization with System.Text.Json
- XML Serialization with XmlSerializer
Chapter Summary
Chapter 15: Design Patterns, Architecture and Professional Practices
Clean Code Principles for C#
- Meaningful Names
- Small Methods
- Consistent Formatting
Naming Conventions and Style Guidelines
Common Design Patterns in C#
- Singleton Pattern
- Factory Pattern
- Strategy Pattern
- Observer Pattern
- Repository Pattern
Layered Architecture
- Example Architecture
Project Organization for Large Solutions
Performance Optimization Strategies
- Common Optimizations
- Profiling Tools
Continuous Integration and Modern Workflows
- CI/CD Pipeline Basics
- Git Best Practices
Your Path Forward as a C# Developer
- Deepen Your Knowledge
- Practice Regularly
- Stay Current
- Build a Portfolio
Final Thoughts
Conclusion
References