You are previewing C#: A Beginner's Guide.
O'Reilly logo
C#: A Beginner's Guide

Book Description

Essential Skills--Made Easy!

C#: A Beginner's Guide offers a step-by-step approach to learning object-oriented programming with C# and the .NET Framework while preparing you for data driven-development. More than 150 easy-to-follow examples are included. The book covers Visual Studio for development and debugging, collections and advanced C# structures, LINQ and traditional database development, as well as file, XML, and JSON handling. By the end of the book, you'll have the foundation you need to begin developing advanced data-driven applications in any C#-based platform.

Designed for Easy Learning

  • Key Skills & Concepts--Chapter-opening lists of specific skills covered in the chapter
  • Try This--Hands-on exercises that show you how to apply your skills
  • Notes--Extra information related to the topic being covered
  • Tips--Helpful reminders or alternate ways of doing things
  • Cautions--Errors and pitfalls to avoid
  • Self Test--End-of-chapter quizzes to reinforce your skills
  • Example code with commentary that describes the programming techniques being illustrated

Ready-to-use code at www.mhprofessional.com

Table of Contents

  1. Cover
  2. Title Page
  3. Copyright Page
  4. About the Author
  5. Dedication
  6. Contents
  7. Acknowledgments
  8. Introduction
  9. Part I Programming Basics
    1. 1 Introduction to C# Coding and Debugging
      1. Brief History of C#
      2. Understanding Basic Code Terminology
        1. Comments
        2. Syntax
        3. Indentation
      3. Writing and Running a Program
        1. Writing and Editing Your Code
        2. Program Compilation and Execution
      4. Try This 1-1: Creating Your First C# Program
      5. Using the Integrated Development Environment
        1. Creating a Console Application Project
        2. Compiling and Running a Program
        3. Stopping the Application
        4. Saving the Solution
        5. Exiting the Solution
        6. Opening the Solution
        7. Renaming a Code File
        8. Renaming Code Structures
      6. Incorporating Debugging Techniques
        1. Errors and Warnings
        2. Breakpoints
        3. Resuming Program Execution
        4. Tooltips
        5. Stepping into Methods
        6. Stepping over Methods
        7. The Call Stack Window
        8. The Locals Window
      7. Try This 1-2: Inspecting Your Code
        1. The Watch Window
        2. Try-Catch Blocks
        3. Logging Data
      8. Chapter 1 Self Test
    2. 2 Variables, Constants, Arrays, and Enumerations
      1. Data Types
        1. Numeric Literals
      2. Variables
        1. Variable Types
        2. Variable Syntax
        3. Variable Names
      3. Constants
        1. Constant Syntax
        2. Constant Names
      4. Try This 2-1: Practice with Variable and Constant Structures
        1. Explicit Variable Conversion (Casting)
        2. Implicit Variable Conversion
      5. Arrays
        1. Array Syntax
      6. Try This 2-2: Practice with Arrays
        1. Array Length
        2. Sorting Arrays
        3. Multidimensional Arrays
      7. Enumerations
        1. Enumeration Syntax
        2. Enumeration Naming
        3. Enumeration Methods
      8. Chapter 2 Self Test
    3. 3 Numeric Operators and Routines
      1. Basic Calculations
        1. Addition
        2. Subtraction
        3. Multiplication
        4. Division
        5. Remainder
        6. Exponentiation
        7. Square Root
      2. Increment Operator
        1. Pre-Increment Expressions
        2. Post-Increment Expressions
      3. Try This 3-1: Operator Practice
      4. Decrement Operator
        1. Pre-Decrement Expressions
        2. Post-Decrement Expressions
      5. Randomization
        1. The Default Random Constructor
        2. The Seeded Random Constructor
      6. Try This 3-2: Reusing a Seed Value
      7. Chapter 3 Self Test
    4. 4 Methods and Scope
      1. Method Syntax
        1. Calling a Method
        2. Indentation
      2. Try This 4-1: Method Implementation Practice
      3. Method Overloads
      4. Passing Arguments by Value and by Reference
        1. Passing Arguments by Value
        2. Passing Arguments by Reference
        3. Passing Arguments Explicitly by Reference
        4. Passing Uninitialized Variables by Reference
      5. Scope
        1. Class Scope
        2. Method Scope
        3. Block Scope
        4. Variable Precedence
      6. Try This 4-2: Examining Scope
      7. Chapter 4 Self Test
    5. 5 Conditional Structures and Loops
      1. Control Structures
        1. Indentation and Alignment
        2. Nesting
      2. Comparison Operators
        1. Equals and Not Equals Operators
        2. Less Than and Greater Than Operators
        3. AND and OR Operators
      3. Conditional Structures
        1. If-Else Statements
      4. Try This 5-1: Comparison Operator and If-Else Statement Practice
        1. Conditional (Ternary) Operators
        2. Switches
      5. Loops
        1. For Loops
      6. Try This 5-2: Nested Loops
        1. While Loops
        2. Do While Loops
        3. Foreach Loops
        4. Comparing Loop Types
        5. Break Statements
        6. Continue Statement
      7. Chapter 5 Self Test
    6. 6 String Manipulation
      1. Numeric Format Specifiers
        1. Raw Text
        2. Rounding
        3. Rounding with a Thousands Separator
        4. Displaying Local Currency
      2. Parsing Strings
        1. Length
        2. IndexOf()
        3. LastIndexOf()
        4. Substring
        5. Split()
      3. Try This 6-1: String Formatting and Parsing Exercise
        1. Join()
        2. StartsWith()
        3. Contains()
      4. Editing Strings
        1. Trim()
        2. Concatenation
        3. Adjusting Letter Case
        4. Replace()
        5. Insert()
        6. Remove()
      5. Escape Sequences
        1. Verbatim String Literals
      6. Regular Expressions
        1. Introductory Expressions
        2. More Regular Expression Operators
      7. Try This 6-2: Regular Expression Exercise
      8. Converting Strings to Other Formats
        1. Convert
        2. TryParse()
      9. Chapter 6 Self Test
    7. 7 Date and Time Handling
      1. TimeSpan Syntax
        1. TimeSpan Properties
      2. TimeSpan Calculations
        1. TimeSpan Addition
        2. TimeSpan Subtraction
        3. TimeSpan Comparison
      3. Try This 7-1: TimeSpan Practice
      4. DateTime Syntax
        1. System Time
        2. DateTime Properties
      5. Formatting DateTime Output
      6. DateTime Calculations
        1. Adding Units of Time
      7. Try This 7-2: DateTime Practice
        1. DateTime Subtraction
        2. DateTime Comparison
      8. Chapter 7 Self Test
  10. Part II Object-Oriented Programming
    1. 8 Encapsulation Through Object-Oriented Programming
      1. Encapsulation
        1. Accessibility
      2. Classes
        1. Objects
        2. Class Declarations
        3. Keyword This
        4. Constructors
      3. Try This 8-1: Class Creation, Constructor, and Accessor Exercise
        1. Properties
        2. Destructors
      4. Static Structures
        1. Declaring Static Structures
      5. Try This 8-2: Examining Static Structures
      6. Structs
      7. Namespaces
      8. Partial Classes
      9. Chapter 8 Self Test
    2. 9 Inheritance
      1. Inheritance Introduction
        1. Declaring a Child Class
        2. Protected Members
      2. Try This 9-1: Inheritance Exercise
        1. Inherited Constructor Overloading
      3. Virtual Inheritance
        1. Declaring Virtual Methods
        2. Overriding Virtual Methods
        3. Declaring Virtual Properties
        4. Overriding Virtual Properties
      4. Abstract Inheritance
        1. Declaring Abstract Classes
        2. Declaring Abstract Methods
        3. Overriding Abstract Methods
        4. Declaring Abstract Properties
        5. Overriding Abstract Properties
      5. Virtual and Abstract Comparison
      6. Try This 9-2: Abstraction Exercise
      7. Polymorphism
      8. Sealed Modifiers
        1. Sealing Classes
        2. Sealing Members
      9. Chapter 9 Self Test
    3. 10 Generic Types and Collections
      1. Generic Types
        1. Declaring Generic Templates
        2. Generic Constructors
        3. Declaring and Initializing Objects of a Generic Class or Struct
        4. Generic Methods
        5. Generic Properties
        6. Generic Variables
      2. Try This 10-1: Practice with Generic Types
      3. List Collections
        1. List Methods
      4. Try This 10-2: Practice with List Collections
      5. ArrayList Collections
        1. Determining Object Types
        2. ArrayList Methods
      6. KeyValuePair Types
      7. Dictionary Collections
        1. Dictionary Methods
        2. Iterating Through Dictionary Items
      8. Hashtable Collections
        1. Hashtable Methods
        2. Iterating Through Hashtable Items
      9. Chapter 10 Self Test
  11. Part III Advanced Structures
    1. 11 Delegates, Anonymous Functions, and Events
      1. Delegates
        1. Delegate Syntax
      2. Try This 11-1: Practice with Delegates
      3. Anonymous Functions
        1. Anonymous Functions with Delegates
      4. Try This 11-2: Practice with Delegates and Anonymous Functions
        1. Anonymous Functions with Func<T,TResult> Delegates
      5. Events
        1. Event Declaration Syntax
        2. Subscribing to an Event
        3. Raising an Event
        4. Unsubscribing from an Event
      6. Chapter 11 Self Test
    2. 12 Interfaces
      1. Interface Syntax
      2. Try This 12-1: Interface Exercise
      3. Try This 12-2: Generating Code from the Interface
        1. Explicit Interface Implementation
        2. Implicit Interface Implementation
      4. Interface Hierarchies
      5. Generic Interfaces
      6. Existing .NET Interfaces
        1. IReadOnlyCollection
        2. IComparable
      7. Chapter 12 Self Test
  12. Part IV Data-Driven Development
    1. 13 Database Setup
      1. Accessing Database Resources for This Book
      2. Creating a Database Connection String
      3. Browsing Database Content with Visual Studio
      4. Try This 13-1: Examining Table Data
        1. Examining Database Column Detail
      5. Try This 13-2: Examining Database Column Detail
      6. Chapter 13 Self Test
    2. 14 The Entity Framework
      1. The Entity Data Model
        1. Adding an Entity Data Model to Your Code Project
      2. Querying Database Objects with the Entity Data Model
        1. DbContext
        2. Entities
        3. DbSet
        4. Querying Entities
      3. Try This 14-1: Querying Entities with the Entity Data Model
      4. Updating the Entity Data Model
        1. Replacing the Entity Data Model
      5. LINQ to Entities Introduction
        1. Lazy Loading and Deferred Execution
        2. Immediate (Eager) Data Loading
        3. LINQPad
      6. Try This 14-2: Observing Query Differences with LINQPad
      7. Chapter 14 Self Test
    3. 15 LINQ to Entities: Query Syntax
      1. Read Queries
        1. Referencing Properties of an Object
        2. Assigning Custom Property Names
      2. Try This 15-1: Reading Data with Query Syntax
        1. Lazy Loading vs. Immediate Data Loading
        2. Filtering
        3. Reading One Object Only
        4. Sorting
        5. Subselection
      3. Try This 15-2: Inspecting Combined Queries
        1. Merging Similar Queries with a Union
        2. Aggregate Summary Queries
      4. Adding Objects
      5. Updating Objects
      6. Deleting Objects
      7. Chapter 15 Self Test
    4. 16 LINQ to Entities: Method-Based Syntax
      1. Lambda Syntax
      2. Read Queries
        1. Filtering
      3. Try This 16-1: Basic Extension Methods
        1. Filtering with a Collection of Values
        2. Retrieving Similar Matches
        3. Selecting Distinct Rows
        4. Sorting
        5. Subselection
        6. Merging Similar Queries with a Union
        7. Aggregate Summary Queries
      4. Try This 16-2: Aggregate Queries with Simple Grouping
      5. Inserting, Updating, and Deleting Objects
      6. Chapter 16 Self Test
    5. 17 Join Queries with LINQ to Entities
      1. Introductory Relational Database Theory
        1. Primary Keys
        2. Composite Primary Key
        3. Foreign Keys
        4. Cardinality
      2. Navigation Properties
      3. Try This 17-1: Testing Your Knowledge of Database Theory
      4. Equal Joins
        1. Equal Joins with Query Syntax
        2. Equal Joins with Method-Based Syntax
        3. Equal Joins with Navigation Properties
      5. Try This 17-2: Practice with Equal Joins
        1. Equal Joins on Composite Keys
      6. Outer Joins
        1. Left Outer Joins
        2. Right Outer Joins
        3. Full Outer Joins
      7. Joining More Than Two Collections in One Query
      8. Bridging Many-to-Many Relationships
        1. Querying Bridge Table Objects
        2. Inserting Bridge Table Objects
        3. Deleting Bridge Table Objects
      9. Chapter 17 Self Test
    6. 18 Traditional Database Development
      1. SQL
      2. Stored Procedures
      3. .NET Classes for Managing Database Data
        1. DataTable
        2. DataColumn
        3. DataRow
      4. Try This 18-1: Constructing and Using Your Own DataTable
      5. Referencing Your Connection String in C#
      6. Memory-Based Database Management with the SqlDataAdapter Class
        1. Initializing a SqlDataAdapter Object
        2. Retrieving Data with the SqlDataAdapter Class
        3. Timing Out
      7. Try This 18-2: Executing SQL from Your C# Code
        1. Managing Stored Procedures with the SqlDataAdapter Class
      8. Streaming Data with the SqlDataReader Class
        1. Managing the Database Connection
      9. Chapter 18 Self Test
    7. 19 File Input Output
      1. Writing and Reading Plain Text
        1. StreamWriter
        2. StreamReader
      2. Try This 19-1: Writing, Reading, and Extracting Text Data
      3. Writing and Reading Binary Data
        1. FileMode
        2. BinaryWriter
        3. BinaryReader
      4. Try This 19-2: Writing and Reading Binary Data
      5. Reading Buffered Binary Data
      6. Randomly Accessing Binary Data
      7. Chapter 19 Self Test
    8. 20 XML Handling
      1. XML Introduction
        1. XML Elements and Attributes
        2. Element Hierarchy
        3. Adding an XML Document to a Visual Studio Project
        4. Well-Formed XML
      2. LINQ to XML Introduction
        1. XElement
        2. XAttribute
        3. Querying Elements
      3. Try This 20-1: Querying XML with LINQ
        1. Additional Element Selection Methods and Properties
      4. Try This 20-2: Querying XML with LINQ, Continued
      5. Creating XML Elements and Attributes
        1. Creating Attributes
        2. Creating Elements
        3. Adding Elements to Existing Documents
      6. Updating XML Elements and Attributes
      7. Deleting XML Elements
      8. XML Serialization and Deserialization
        1. Serialization
        2. Deserialization
      9. Chapter 20 Self Test
    9. 21 JSON Handling
      1. JSON Introduction
        1. Json.NET
      2. Creating Dynamic JSON Objects
        1. Dynamic Types
        2. Creating JSON Arrays
      3. Try This 21-1: JSON Object and Array Creation
      4. Dynamic Parsing
        1. Referencing Properties
        2. Referencing Arrays
      5. Serialization
      6. Try This 21-2: Creating JSON Through Serialization
        1. Customizing Property Names During Serialization
      7. Deserialization
        1. Mapping to Custom Property Names During Deserialization
      8. Chapter 21 Self Test
  13. Part V Stand-alone Topics
    1. 22 Attributes
      1. .NET Framework Attributes
      2. Try This 22-1: Implementing Attributes Practice
      3. Creating and Implementing Custom Attributes
        1. Inheriting from the Attribute Class
        2. Optional AttributeUsage Attributes
        3. Implementing Custom Attributes
        4. Querying Attributes
      4. Try This 22-2: Implementing and Querying Custom Attributes
      5. Reflection Introduction
      6. Querying Attribute Targets
      7. Chapter 22 Self Test
    2. 23 Operator Overloading
      1. Operator Overloading Introduction
      2. Operator Overload Method Requirements
      3. Operators that Can Be Overloaded
      4. Try This 23-1: Creating and Using a * Operator Overload
      5. Try This 23-2: Creating and Using a – Operator Overload
      6. Chapter 23 Self Test
  14. A FoodStore Database Reference
  15. Index