You are previewing Learning C# 3.0.
O'Reilly logo
Learning C# 3.0

Book Description

If you're new to C#, this popular book is the ideal way to get started. Completely revised for the latest version of the language, Learning C# 3.0 starts with the fundamentals and takes you through intermediate and advanced C# features -- including generics, interfaces, delegates, lambda expressions, and LINQ. You'll also learn how to build Windows applications and handle data with C#. No previous programming experience is required -- in fact, if you've never written a line of code in your life, bestselling authors Jesse Liberty and Brian MacDonald will show you how it's done. Each chapter offers a self-contained lesson to help you master key concepts, with plenty of annotated examples, illustrations, and a concise summary. With this book, you will:

  • Learn how to program as you learn C#

  • Grasp the principles of object-oriented programming through C#

  • Discover how to use the latest features in C# 3.0 and the .NET 3.5 Framework--including LINQ and the Windows Presentation Foundation (WPF)

  • Create Windows applications and data-driven applications

You'll also find a unique Test Your Knowledge section in each chapter, with practical exercises and review quizzes, so you can practice new skills and test your understanding. If you're ready to dive into C# and .NET programming, this book is a great way to quickly get up to speed.

Table of Contents

  1. Learning C# 3.0
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. About This Book
      2. Who This Book Is For
      3. How This Book Is Organized
      4. Conventions Used in This Book
      5. Support: A Note from Jesse Liberty
      6. Using Code Examples
      7. We'd Like to Hear from You
      8. Safari® Books Online
      9. Acknowledgments
        1. Jesse Liberty
        2. Brian MacDonald
    4. 1. C# and .NET Programming
      1. Installing C# Express
      2. C# 3.0 and .NET 3.5
      3. The .NET Platform
      4. The .NET Framework
      5. The C# Language
      6. Your First Program: Hello World
      7. The Compiler
      8. Examining Your First Program
      9. The Integrated Development Environment
      10. Summary
      11. Test Your Knowledge: Quiz
      12. Test Your Knowledge: Exercise
    5. 2. Visual Studio 2008 and C# Express 2008
      1. Before You Read Further
      2. The Start Page
      3. Projects and Solutions
        1. Project Types
        2. Templates
      4. Inside the Integrated Development Environment
      5. Building and Running Applications
      6. Menus
        1. The File Menu
        2. The Edit Menu
          1. The Clipboard Ring
          2. Find and Replace
          3. Go To
          4. Insert File As Text
          5. Advanced
          6. Incremental search
          7. Bookmarks
          8. Outlining
          9. IntelliSense
        3. The View Menu
          1. Class View
          2. Code Definition
          3. Error List
          4. Output
          5. Properties
          6. Task List
          7. Toolbox
          8. Other Windows
        4. The Refactor Menu
        5. The Project Menu
        6. The Build Menu
        7. The Debug Menu
        8. The Data Menu
        9. The Format Menu
        10. The Tools Menu
          1. Connect to Device
          2. Device Emulator Manager
          3. Connect to Database
          4. Connect to Server
          5. Code Snippets Manager
          6. Choose Toolbox Items
          7. External Tools
          8. Import and Export Settings
          9. Options
        11. The Window Menu
        12. The Help Menu
      7. Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Exercises
    6. 3. C# Language Fundamentals
      1. Statements
      2. Types
        1. Numeric Types
        2. Nonnumeric Types: char and bool
        3. Types and Compiler Errors
      3. WriteLine( ) and Output
      4. Variables and Assignment
        1. Definite Assignment
        2. Implicitly Typed Variables
      5. Casting
      6. Constants
        1. Literal Constants
        2. Symbolic Constants
        3. Enumerations
      7. Strings
      8. Whitespace
      9. Summary
      10. Test Your Knowledge: Quiz
      11. Test Your Knowledge: Exercises
    7. 4. Operators
      1. Expressions
      2. The Assignment Operator (=)
      3. Mathematical Operators
        1. Simple Arithmetic Operators (+, –, *, /)
        2. The Modulus Operator (%)
      4. Increment and Decrement Operators
        1. The Calculate and Reassign Operators
        2. Increment or Decrement by 1
        3. The Prefix and Postfix Operators
      5. Relational Operators
      6. Logical Operators and Conditionals
      7. The Conditional Operator
      8. Operator Precedence
      9. Summary
      10. Test Your Knowledge: Quiz
      11. Test Your Knowledge: Exercises
    8. 5. Branching
      1. Unconditional Branching Statements
      2. Conditional Branching Statements
        1. if Statements
        2. Single-Statement if Blocks
        3. Short-Circuit Evaluation
        4. if . . . else Statements
        5. Nested if Statements
        6. switch Statements
        7. Fall-Through and Jump-to Cases
        8. Switch on string Statements
      3. ReadLine( ) and Input
      4. Iteration (Looping) Statements
        1. Creating Loops with goto
        2. The while Loop
        3. The do…while Loop
        4. The for Loop
          1. Controlling a for loop with the modulus operator
          2. Breaking out of a for loop
          3. The continue statement
          4. Optional for loop header elements
      5. Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Exercises
    9. 6. Object-Oriented Programming
      1. Creating Models
      2. Classes and Objects
      3. Defining a Class
      4. Class Relationships
      5. The Three Pillars of Object-Oriented Programming
        1. Encapsulation
        2. Specialization
        3. Polymorphism
      6. Object-Oriented Analysis and Design
      7. Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Exercises
    10. 7. Classes and Objects
      1. Defining Classes
        1. Instantiating Objects
        2. Creating a Box Class
        3. Access Modifiers
      2. Method Arguments
      3. Return Types
      4. Constructors
      5. Initializers
      6. Object Initializers
      7. Anonymous Types
      8. The this Keyword
      9. Static and Instance Members
        1. Invoking Static Methods
        2. Using Static Fields
      10. Finalizing Objects
      11. Memory Allocation: The Stack Versus the Heap
      12. Summary
      13. Test Your Knowledge: Quiz
      14. Test Your Knowledge: Exercises
    11. 8. Inside Methods
      1. Overloading Methods
      2. Encapsulating Data with Properties
        1. The get Accessor
        2. The set Accessor
        3. Automatic Properties
      3. Returning Multiple Values
        1. Passing Value Types by Reference
        2. out Parameters and Definite Assignment
      4. Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Exercises
    12. 9. Basic Debugging
      1. Setting a Breakpoint
        1. Using the Debug Menu to Set Your Breakpoint
        2. Setting Conditions and Hit Counts
      2. Examining Values: The Autos and Locals Windows
      3. Setting Your Watch
      4. The Call Stack
      5. Stopping Debugging
      6. Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Exercises
    13. 10. Arrays
      1. Using Arrays
        1. Declaring Arrays
        2. Understanding Default Values
        3. Accessing Array Elements
        4. Arrays and Loops
      2. The foreach Statement
      3. Initializing Array Elements
      4. The params Keyword
      5. Multidimensional Arrays
        1. Rectangular Arrays
        2. Jagged Arrays
      6. Array Methods
      7. Sorting Arrays
      8. Summary
      9. Test Your Knowledge: Quiz
      10. Test Your Knowledge: Exercises
    14. 11. Inheritance and Polymorphism
      1. Specialization and Generalization
      2. Inheritance
        1. Implementing Inheritance
        2. Calling the Base Class Constructor
        3. Hiding the Base Class Method
        4. Controlling Access
      3. Polymorphism
        1. Creating Polymorphic Types
        2. Overriding Virtual Methods
        3. Using Objects Polymorphically
        4. Versioning with new and override
      4. Abstract Classes
      5. Sealed Classes
      6. The Root of All Classes: Object
      7. Summary
      8. Test Your Knowlege: Quiz
      9. Test Your Knowledge: Exercises
    15. 12. Operator Overloading
      1. Designing the Fraction Class
      2. Using the operator Keyword
      3. Creating Useful Operators
      4. The Equals Operator
      5. Conversion Operators
      6. Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Exercises
    16. 13. Interfaces
      1. What Interfaces Are
      2. Implementing an Interface
        1. Defining the Interface
        2. Implementing the Interface on the Client
      3. Implementing More Than One Interface
      4. Casting to an Interface
      5. The is and as Operators
      6. Extending Interfaces
      7. Combining Interfaces
      8. Overriding Interface Methods
      9. Explicit Interface Implementation
      10. Summary
      11. Test Your Knowledge: Quiz
      12. Test Your Knowledge: Exercises
    17. 14. Generics and Collections
      1. Generics
      2. Collection Interfaces
      3. Creating Your Own Collections
        1. Creating Indexers
        2. Indexers and Assignment
        3. Indexing on Other Values
        4. Generic Collection Interfaces
        5. The IEnumerable<T> Interface
      4. Framework Generic Collections
        1. Generic Lists: List<T>
          1. Sorting objects with the generic list
          2. Controlling sorting by implementing IComparer<T>
        2. Generic Queues
        3. Generic Stacks
        4. Dictionaries
      5. Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Exercises
    18. 15. Strings
      1. Creating Strings
        1. String Literals
        2. Escape Characters
        3. Verbatim Strings
        4. The ToString( ) Method
      2. Manipulating Strings
        1. Comparing Strings
        2. Concatenating Strings
        3. Copying Strings
        4. Testing for Equality
        5. Other Useful String Methods
        6. Finding Substrings
        7. Splitting Strings
        8. The StringBuilder Class
      3. Regular Expressions
      4. The Regex Class
      5. Summary
      6. Test Your Knowledge: Quiz
      7. Test Your Knowledge: Exercises
    19. 16. Throwing and Catching Exceptions
      1. Bugs, Errors, and Exceptions
      2. Throwing Exceptions
      3. Searching for an Exception Handler
      4. The throw Statement
      5. The try and catch Statements
      6. How the Call Stack Works
      7. Creating Dedicated catch Statements
      8. The finally Statement
      9. Exception Class Methods and Properties
      10. Custom Exceptions
      11. Summary
      12. Test Your Knowledge: Quiz
      13. Test Your Knowledge: Exercises
    20. 17. Delegates and Events
      1. Delegates
        1. Events
        2. Publishing and Subscribing
        3. Events and Delegates
        4. Solving Delegate Problems with Events
        5. The event Keyword
      2. Using Anonymous Methods
      3. Lambda Expressions
      4. Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Exercises
    21. 18. Creating Windows Applications
      1. Creating a Simple Windows Form
        1. Using the Visual Studio Designer
      2. Creating a Real-World Application
        1. Creating the Basic UI Form
        2. Populating the TreeView Controls
          1. TreeNode objects
          2. Recursing through the subdirectories
          3. Getting the files in the directory
        3. Handling the TreeView Events
          1. Clicking the source TreeView
          2. Expanding a directory
          3. Clicking the target TreeView
        4. Handling the Button Events
          1. Handling the Clear button event
          2. Implementing the Copy button event
          3. Handling the Delete button event
          4. Handling the Cancel button event
      3. Source Code
      4. Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Exercises
    22. 19. Windows Presentation Foundation
      1. Your First WPF Application
      2. WPF Differences from Windows Forms
      3. Using Resources
      4. Animations
        1. Triggers and Storyboards
        2. Animations As Resources
      5. C# and WPF
        1. Grids and Stack Panels
          1. Defining ListBox styles
          2. Triggers and animations
        2. Adding Data
          1. Instantiating objects declaratively
        3. Using the Data in the XAML
        4. Defining the ListBox
        5. Event Handling
        6. The Complete XAML File
      6. Summary
      7. Test Your Knowledge: Quiz
      8. Test Your Knowledge: Exercises
    23. 20. ADO.NET and Relational Databases
      1. Relational Databases and SQL
        1. Installing the Northwind Database
        2. Tables, Records, and Columns
        3. Normalization
        4. Declarative Referential Integrity
        5. SQL
      2. The ADO.NET Object Model
        1. DataTables and DataColumns
        2. DataRelations
        3. Rows
        4. DataAdapter
        5. DbCommand and DbConnection
        6. DataReader
      3. Getting Started with ADO.NET
      4. Summary
      5. Test Your Knowledge: Quiz
      6. Test Your Knowledge: Exercises
    24. 21. LINQ
      1. Querying In-Memory Data
      2. Anonymous Types and Implicitly Typed Variables
      3. Lambda Expressions
      4. Ordering and Joining
      5. Using LINQ with SQL
      6. Using the Object Relational Designer
      7. Summary
      8. Test Your Knowledge: Quiz
      9. Test Your Knowledge: Exercises
    25. A. Answers to Quizzes and Exercises
      1. Chapter 1: C# and .NET Programming
        1. Quiz Solutions
        2. Exercise Solution
      2. Chapter 2: Visual Studio 2008 and C# Express 2008
        1. Quiz Solutions
        2. Exercise Solutions
      3. Chapter 3: C# Language Fundamentals
        1. Quiz Solutions
        2. Exercise Solutions
      4. Chapter 4: Operators
        1. Quiz Solutions
        2. Exercise Solutions
      5. Chapter 5: Branching
        1. Quiz Solutions
        2. Exercise Solutions
      6. Chapter 6: Object-Oriented Programming
        1. Quiz Solutions
        2. Exercise Solutions
      7. Chapter 7: Classes and Objects
        1. Quiz Solutions
        2. Exercise Solutions
      8. Chapter 8: Inside Methods
        1. Quiz Solutions
        2. Exercise Solutions
      9. Chapter 9: Basic Debugging
        1. Quiz Solutions
        2. Exercise Solutions
      10. Chapter 10: Arrays
        1. Quiz Solutions
        2. Exercise Solutions
      11. Chapter 11: Inheritance and Polymorphism
        1. Quiz Solutions
        2. Exercise Solutions
      12. Chapter 12: Operator Overloading
        1. Quiz Solutions
        2. Exercise Solutions
      13. Chapter 13: Interfaces
        1. Quiz Solutions
        2. Exercise Solutions
      14. Chapter 14: Generics and Collections
        1. Quiz Solutions
        2. Exercise Solutions
      15. Chapter 15: Strings
        1. Quiz Solutions
        2. Exercise Solutions
      16. Chapter 16: Throwing and Catching Exceptions
        1. Quiz Solutions
        2. Exercise Solutions
      17. Chapter 17: Delegates and Events
        1. Quiz Solutions
        2. Exercise Solutions
      18. Chapter 18: Creating Windows Applications
        1. Quiz Solutions
        2. Exercise Solutions
      19. Chapter 19: Windows Presentation Foundation
        1. Quiz Solutions
        2. Exercise Solutions
      20. Chapter 20: ADO.NET and Relational Databases
        1. Quiz Solutions
        2. Exercise Solutions
      21. Chapter 21: LINQ
        1. Quiz Solutions
        2. Exercise Solutions
    26. Index
    27. About the Authors
    28. Colophon
    29. SPECIAL OFFER: Upgrade this ebook with O’Reilly