C# 3.0 Cookbook, 3/e (Paerback)
暫譯: C# 3.0 食譜,第3版 (平裝本)

Jay Hilyard, Stephen Teilhet

  • 出版商: O'Reilly
  • 出版日期: 2007-12-01
  • 售價: $2,060
  • 貴賓價: 9.5$1,957
  • 語言: 英文
  • 頁數: 896
  • 裝訂: Paperback
  • ISBN: 059651610X
  • ISBN-13: 9780596516109
  • 相關分類: C#
  • 已過版

買這商品的人也買了...

相關主題

商品描述

Description 

Completely updated for C# 3.0 and the .NET 3.5 platform, the new edition of this bestseller offers more than 250 code recipes to common and not-so-common problems that C# programmers face every day. Every recipe in the book has been reconsidered with more than a third of them rewritten to take advantage of new C# 3.0 features. If you prefer solutions you can use today to general C# language instruction, and quick answers to theory, this is your book.

C# 3.0 Cookbook offers a new chapter on LINQ (language integrated query), plus two expanded chapters for recipes for extension methods, lambda functions, object initializers, new synchronization primitives and more. The new edition is also complemented by a public wiki, which not only includes all of the C# 2.0 recipes from the previous edition unchanged by the release of C# 3.0, but invites you to suggest better ways to solve those tasks.

Here are some of topics covered:
  • LINQ
  • Numeric data types and Enumerations
  • Strings and characters
  • Classes and structures
  • Generics
  • Collections
  • Exception handling
  • Delegates, events, and lambda expressions
  • Filesystem interactions
  • Web site access
  • XML usage (including LINQ to XML, XPath and XSLT)
  • Networking
  • Threading
  • Data Structures & Algorithms
Each recipe in the book includes tested code that you can download from oreilly.com and reuse in your own applications, and each one includes a detailed discussion of how and why the underling technology works. You don't have to be an experienced C# or .NET developer to use C# 3.0 Cookbook. You just have to be someone who wants to solve a problem now, without having to learn all the related theory first.
 

Table of Contents

Preface

1. Language Integrated Query (LINQ)

      1.1 Query a Message Queue 

      1.2 Using Set Semantics with Data 

      1.3 Reuse Parameterized Queries with LINQ to SQL 

      1.4 Sort Results in a Culture-Sensitive Manner 

      1.5 Adding Functional Extensions for Use with LINQ 

      1.6 Query and Join Across Data Repositories 

      1.7 Querying Configuration Files with LINQ 

      1.8 Creating XML Straight from a Database 

      1.9 Being Selective About Your Query Results 

      1.10 Using LINQ with Collections That Don't Support IEnumerable<T> 

2. Strings and Characters

      2.1 Determining the Kind of Character a Char Contains 

      2.2 Controlling Case Sensitivity When Comparing Two Characters 

      2.3

       Within Another String 

      2.4 Controlling Case Sensitivity When Comparing Two Strings 

      2.5 Comparing a String to the Beginning or End of a Second String 

      2.6 Inserting Text into a String 

      2.7 Removing or Replacing Characters Within a String 

      2.8 Encoding Binary Data As Base64 

      2.9 Decoding a Base64-Encoded Binary 

      2.10 Converting a String Returned As a Byte[ ] Back into a String 

      2.11 Passing a String to a Method That Accepts Only a Byte[ ] 

      2.12 Converting Strings to Other Types 

      2.13 Creating a Delimited String 

      2.14 Extracting Items from a Delimited String 

      2.15 Iterating over Each Character in a String 

      2.16 Pruning Characters from the Head and/or Tail of a String 

      2.17 Testing a String for Null or Empty 

      2.18 Appending a Line 

3. Classes and Structures

      3.1 Creating Union-Type Structures 

      3.2 Making a Type Sortable 

      3.3 Making a Type Searchable 

      3.4 Indirectly Overloading the +=, -=, /=, and *= Operators 

      3.5 Indirectly Overloading the &&, ||, and ?: Operators 

      3.6 Making Error-Free Expressions 

      3.7 Reducing Your Boolean Logic 

      3.8

       Language-Agnostic Manner 

      3.9

       or the is Operator 

      3.10 Casting with the as Operator 

      3.11 Determining a Variable's Type with the is Operator 

      3.12 Returning Multiple Items from a Method 

      3.13 Parsing Command-Line Parameters 

      3.14 Initializing a Constant Field at Runtime 

      3.15 Building Cloneable Classes 

      3.16 Assuring an Object's Disposal 

      3.17 Disposing of Unmanaged Resources 

      3.18 Determining Where Boxing and Unboxing Occur 

4. Generics

      4.1 Deciding When and Where to Use Generics 

      4.2 Understanding Generic Types 

      4.3 Replacing the ArrayList with Its Generic Counterpart 

      4.4 Replacing the Stack and Queue with Their Generic Counterparts 

      4.5 Using a Linked List 

      4.6 Creating a Value Type That Can Be Initialized to Null 

      4.7 Reversing the Contents of a Sorted List 

      4.8 Making Read-Only Collections the Generic Way 

      4.9 Replacing the Hashtable with Its Generic Counterpart 

      4.10 Using foreach with Generic Dictionary Types 

      4.11 Constraining Type Arguments 

      4.12 Initializing Generic Variables to Their Default Values 

5. Collections

      5.1 Swapping Two Elements in an Array 

      5.2 Reversing an Array Quickly 

      5.3 Writing a More Flexible StackTrace Class 

      5.4 Determining the Number of Times an Item Appears in a List<T> 

      5.5 Retrieving All Instances of a Specific Item in a List<T> 

      5.6 Inserting and Removing Items from an Array 

      5.7 Keeping Your List<T> Sorted 

      5.8 Sorting a Dictionary's Keys and/or Values 

      5.9 Creating a Dictionary with Max and Min Value Boundaries 

      5.10 Storing Snapshots of Lists in an Array 

      5.11 Persisting a Collection Between Application Sessions 

      5.12 Testing Every Element in an Array or List<T> 

      5.13 Performing an Action on Each Element in an Array or List<T> 

      5.14 Creating a Read-Only Array or List<T> 

6. Iterators, Partial Types, and Partial Methods

      6.1 Creating an Iterator on a Generic Type 

      6.2 Creating an Iterator on a Nongeneric Type 

      6.3 Creating Custom Enumerators 

      6.4 Implementing Iterator Logic 

      6.5 Forcing an Iterator to Stop Iterating 

      6.6 Dealing with Finally Blocks and Iterators 

      6.7 Implementing Nested foreach Functionality in a Class 

      6.8 Organizing Your Interface Implementations 

      6.9 Generating Code That Is No Longer in Your Main Code Paths 

      6.10 Adding Hooks to Generated Entities 

7. Exception Handling

      7.1 Knowing When to Catch and Rethrow Exceptions 

      7.2 Assuring Exceptions Are Not Lost When Using Finally Blocks 

      7.3 Handling Exceptions Thrown from Methods Invoked via Reflection 

      7.4 Preventing Unhandled Exceptions 

      7.5 Getting Exception Information 

      7.6 Getting to the Root of a Problem Quickly 

      7.7 Creating a New Exception Type 

      7.8 Obtaining a Stack Trace 

      7.9 Breaking on a First-Chance Exception 

      7.10 Handling Exceptions Thrown from an Asynchronous Delegate 

      7.11 Giving Exceptions the Extra Info They Need with Exception.Data
      7.12 Dealing with Unhandled Exceptions in WinForms Applications 

      7.13

       Foundation (WPF) Applications 

      7.14 Analyzing Exceptions for Common Errors 

8. Diagnostics

      8.1 Providing Fine-Grained Control over Debugging/Tracing Output 

      8.2 Determining Whether a Process Has Stopped Responding 

      8.3 Using Event Logs in Your Application 

      8.4 Searching Event Log Entries 

      8.5 Watching the Event Log for a Specific Entry 

      8.6 Implementing a Simple Performance Counter 

      8.7 Enabling and Disabling Complex Tracing Code 

      8.8 Capturing Standard Output for a Process 

      8.9 Creating Custom Debugging Displays for Your Classes 

9. Delegates, Events, and Lambda Expressions

      9.1

       Multicast Delegate 

      9.2

       Multicast Delegate 

      9.3

       Multicast Delegate 

      9.4

       Asynchronous 

      9.5 An Advanced Interface Search Mechanism 

      9.6 Observing Additions and Modifications to Dictionaries 

      9.7 Using Lambda Expressions 

      9.8 Set Up Event Handlers Without the Mess 

      9.9 Using Different Parameter Modifiers in Lambda Expressions 

      9.10 Using Closures in C# 

      9.11 Performing Multiple Operations on a List Using Functors 

10. Regular Expressions

      10.1 Enumerating Matches 

      10.2 Extracting Groups from a MatchCollection 

      10.3 Verifying the Syntax of a Regular Expression 

      10.4 Quickly Finding Only the Last Match in a String 

      10.5 Augmenting the Basic String Replacement Function 

      10.6 Implementing a Better Tokenizer 

      10.7 Counting Lines of Text 

      10.8 Returning the Entire Line in Which a Match Is Found 

      10.9 Finding a Particular Occurrence of a Match 

      10.10 Using Common Patterns 

11. Data Structures and Algorithms

      11.1 Creating a Hash Code for a Data Type 

      11.2 Creating a Priority Queue 

      11.3 Creating a One-to-Many Map (MultiMap) 

      11.4 Creating a Binary Search Tree 

      11.5 Creating an n-ary Tree 

      11.6 Using a HashSet Object 

12. Filesystem I/O

      12.1 Manipulating File Attributes 

      12.2 Renaming a File 

      12.3 Outputting a Platform-Independent EOL Character 

      12.4 Manipulating Directory Attributes 

      12.5 Renaming a Directory 

      12.6 Searching for Directories or Files Using Wildcards 

      12.7 Obtaining the Directory Tree 

      12.8 Parsing a Path 

      12.9 Parsing Paths in Environment Variables 

      12.10 Launching and Interacting with Console Utilities 

      12.11 Locking Subsections of a File 

      12.12 Waiting for an Action to Occur in the Filesystem 

      12.13 Comparing Version Information of Two Executable Modules 

      12.14 Querying Information for All Drives on a System 

      12.15 Compressing and Decompressing Your Files 

13. Reflection

      13.1 Listing Referenced Assemblies 

      13.2 Listing Exported Types 

      13.3 Finding Overridden Methods 

      13.4 Finding Members in an Assembly 

      13.5 Determining and Obtaining Nested Types Within an Assembly 

      13.6 Displaying the Inheritance Hierarchy for a Type 

      13.7 Finding the Subclasses of a Type 

      13.8 Finding All Serializable Types Within an Assembly 

      13.9 Dynamically Invoking Members 

      13.10 Determining If a Type or Method  Is Generic 

      13.11 Accessing Local Variable Information 

      13.12 Creating a Generic Type 

14. Web

      14.1 Converting an IP Address to a Hostname 

      14.2 Converting a Hostname to an IP Address 

      14.3 Parsing a URI 

      14.4 Handling Web Server Errors 

      14.5 Communicating with a Web Server 

      14.6 Going Through a Proxy 

      14.7 Obtaining the HTML from a URL 

      14.8 Using the Web Browser Control 

      14.9 Tying Database Tables to the Cache 

      14.10 Prebuilding an ASP.NET Web Site Programmatically 

      14.11 Escaping and Unescaping Data for the Web 

      14.12 Using the UriBuilder Class 

      14.13 Inspect and Change Your Web Application Configuration 

      14.14

       Faster Performance 

      14.15 Checking Out a Web Server's Custom Error Pages 

15. XML

      15.1 Reading and Accessing XML Data in Document Order 

      15.2 Reading XML on the Web 

      15.3 Querying the Contents of an XML Document 

      15.4 Validating XML 

      15.5 Creating an XML Document Programmatically 

      15.6 Detecting Changes to an XML Document 

      15.7 Handling Invalid Characters in an XML String 

      15.8 Transforming XML 

      15.9 Tearing Apart an XML Document 

      15.10 Putting Together an XML Document 

      15.11 Validating Modified XML Documents Without Reloading 

      15.12 Extending Transformations 

      15.13 Getting Your Schemas in Bulk from Existing XML Files 

      15.14 Passing Parameters to Transformations 

16. Networking

      16.1 Writing a TCP Server 

      16.2 Writing a TCP Client 

      16.3 Simulating Form Execution 

      16.4 Transferring Data via HTTP 

      16.5 Using Named Pipes to Communicate 

      16.6 Pinging Programmatically 

      16.7 Send SMTP Mail Using the SMTP Service 

      16.8 Use Sockets to Scan the Ports on a Machine 

      16.9 Use the Current Internet Connection Settings 

      16.10 Transferring Files Using FTP 

17. Security

      17.1 Controlling Access to Types in a Local Assembly 

      17.2 Encrypting/Decrypting a String 

      17.3 Encrypting and Decrypting a File 

      17.4 Cleaning Up Cryptography Information 

      17.5

       Following Transmission 

      17.6 Storing Data Securely 

      17.7 Making a Security Assert Safe 

      17.8 Verifying That an Assembly Has Been Granted Specific Permissions 

      17.9 Minimizing the Attack Surface of an Assembly 

      17.10 Obtaining Security/Audit Information 

      17.11 Granting/Revoking Access to a File or Registry Key 

      17.12 Protecting String Data with Secure Strings 

      17.13 Securing Stream Data 

      17.14 Encrypting web.config Information 

      17.15 Obtaining the Full Reason a SecurityException Was Thrown 

      17.16 Achieving Secure Unicode Encoding 

      17.17 Obtaining a Safer File Handle 

18. Threading and Synchronization

      18.1 Creating Per-Thread Static Fields 

      18.2 Providing Thread-Safe Access to Class Members 

      18.3 Preventing Silent Thread Termination 

      18.4 Being Notified of the Completion of an Asynchronous Delegate 

      18.5 Storing Thread-Specific Data Privately 

      18.6 Granting Multiple Access to Resources with a Semaphore 

      18.7 Synchronizing Multiple Processes with the Mutex 

      18.8 Using Events to Make Threads Cooperate 

      18.9 Get the Naming Rights for Your Events 

      18.10 Performing Atomic Operations Among Threads 

      18.11 Optimizing Read-Mostly Access 

19. Toolbox

      19.1

       or User Session Changes 

      19.2 Controlling a Service 

      19.3 List What Processes an Assembly Is Loaded In 

      19.4 Using Message Queues on a Local Workstation 

      19.5 Finding the Path to the Current Framework Version 

      19.6

       Registered in the Global Assembly Cache (GAC) 

      19.7 Capturing Output from the Standard Output Stream 

      19.8 Running Code in Its Own AppDomain 

      19.9

       Version of the Current Operating System 

20. Numbers and Enumerations

      20.1 Converting Between Degrees and Radians 

      20.2 Using the Bitwise Complement Operator with Various Data Types 

      20.3 Converting a Number in Another Base to Base10 

      20.4 Determining Whether a String Is a Valid Number 

      20.5 Rounding a Floating-Point Value 

      20.6 Choosing a Rounding Algorithm 

      20.7 Converting Between Temperature Scales 

      20.8 Safely Performing a Narrowing Numeric Cast 

      20.9 Displaying an Enumeration Value As a String 

      20.10 Converting Plain Text to an Equivalent Enumeration Value 

      20.11 Testing for a Valid Enumeration Value 

      20.12 Testing for a Valid Enumeration of Flags 

      20.13 Using Enumerated Members in a Bit Mask 

      20.14 Determining Whether One or More Enumeration Flags Are Set 

      20.15 Determining the Integral Part of a Decimal or Double 

Index

商品描述(中文翻譯)

描述

本書針對 C# 3.0 和 .NET 3.5 平台進行了全面更新,這本暢銷書的新版本提供了超過 250 個針對 C# 程式設計師每天面對的常見和不常見問題的程式碼範例。書中的每個範例都經過重新考量,超過三分之一的範例已重寫,以利用新的 C# 3.0 特性。如果您更喜歡可以立即使用的解決方案,而不是一般的 C# 語言教學,並且希望快速獲得理論的答案,那麼這本書就是您的選擇。

《C# 3.0 食譜》提供了一個有關 LINQ(語言整合查詢)的新章節,以及兩個擴展章節,涵蓋擴展方法、lambda 函數、物件初始化器、新的同步原語等的範例。新版本還附帶了一個公共維基,不僅包括了前一版本中未因 C# 3.0 發布而改變的所有 C# 2.0 範例,還邀請您提出更好的解決方案。

以下是涵蓋的一些主題:
- LINQ
- 數值資料類型和列舉
- 字串和字元
- 類別和結構
- 泛型
- 集合
- 異常處理
- 委派、事件和 lambda 表達式
- 檔案系統互動
- 網站訪問
- XML 使用(包括 LINQ to XML、XPath 和 XSLT)
- 網路
- 執行緒
- 資料結構與演算法

書中的每個範例都包含可從 oreilly.com 下載並在您自己的應用程式中重用的測試過的程式碼,並且每個範例都包含了如何以及為什麼底層技術運作的詳細討論。您不必是經驗豐富的 C# 或 .NET 開發人員才能使用《C# 3.0 食譜》。您只需是一個希望立即解決問題的人,而不必先學習所有相關理論。

目錄

前言

1. 語言整合查詢 (LINQ)
- 1.1 查詢訊息佇列
- 1.2 使用集合語義處理資料
- 1.3 使用 LINQ to SQL 重用參數化查詢
- 1.4 以文化敏感的方式排序結果
- 1.5 為 LINQ 添加功能擴展
- 1.6 跨資料庫查詢和聯接
- 1.7 使用 LINQ 查詢配置檔
- 1.8 直接從資料庫創建 XML
- 1.9 對查詢結果進行選擇
- 1.10 在不支持 IEnumerable 的集合中使用 LINQ

2. 字串和字元
- 2.1 確定字元的類型
- 2.2 比較兩個字元時控制大小寫敏感性
- 2.3 在另一個字串中
- 2.4 比較兩個字串時控制大小寫敏感性
- 2.5 將字串與第二個字串的開頭或結尾進行比較
- 2.6 在字串中插入文本
- 2.7 移除或替換字串中的字元
- 2.8 將二進位資料編碼為 Base64
- 2.9 解碼 Base64 編碼的二進位資料
- 2.10 將返回的字串轉換為 Byte[] 再轉回字串
- 2.11 將字串傳遞給僅接受 Byte[] 的方法
- 2.12 將字串轉換為其他類型
- 2.13 創建分隔字串
- 2.14 從分隔字串中提取項目
- 2.15 遍歷字串中的每個字元
- 2.16 修剪字串的開頭和/或結尾的字元
- 2.17 測試字串是否為 Null 或空
- 2.18 附加一行

3. 類別和結構
- 3.1 創建聯合類型結構
- 3.2 使類型可排序
- 3.3 使類型可搜尋
- 3.4 間接重載 +=、-=、/= 和 *= 運算子
- 3.5 間接重載 &&、|| 和 ?: 運算子
- 3.6 創建無錯誤的表達式
- 3.7 簡化布林邏輯
- 3.8 以語言無關的方式
- 3.9 或使用 is 運算子
- 3.10 使用 as 運算子進行類型轉換
- 3.11 使用 is 運算子確定變數的類型
- 3.12 從方法返回多個項目
- 3.13 解析命令列參數
- 3.14 在執行時初始化常數欄位
- 3.15 建立可克隆的類別
- 3.16 確保物件的處置
- 3.17 處置非管理資源
- 3.18 確定裝箱和拆箱發生的位置

4. 泛型
- 4.1 決定何時何地使用泛型
- 4.2 理解泛型類型
- 4.3 用其泛型對應物替換 ArrayList
- 4.4 用其泛型對應物替換 Stack 和 Queue
- 4.5 使用鏈結串列
- 4.6 創建可以初始化為 Null 的值類型
- 4.7 反轉已排序列表的內容
- 4.8 以泛型方式創建唯讀集合
- 4.9 用其泛型對應物替換 Hashtable
- 4.10 在泛型字典類型中使用 foreach
- 4.11 限制類型參數
- 4.12 將泛型變數初始化為其預設值

5. 集合
- 5.1 交換陣列中的兩個元素
- 5.2 快速反轉陣列
- 5.3 寫一個更靈活的 StackTrace 類
- 5.4 確定項目在 List 中出現的次數
- 5.5 檢索 List 中特定項目的所有實例
- 5.6 在陣列中插入和移除項目
- 5.7 保持 List 的排序
- 5.8 對字典的鍵和值進行排序
- 5.9 創建具有最大和最小值邊界的字典
- 5.10 在陣列中存儲列表的快照
- 5.11 在應用程式會話之間持久化集合
- 5.12 測試陣列或 List 中的每個元素
- 5.13 對陣列或 List 中的每個元素執行操作
- 5.14 創建唯讀陣列或 List

6. 迭代器、部分類型和部分方法
- 6.1 在泛型類型上創建迭代器
- 6.2 在非泛型類型上創建迭代器
- 6.3 創建自定義列舉器
- 6.4 實現迭代器邏輯
- 6.5 強制迭代器停止迭代
- 6.6 處理 finally 區塊和迭代器
- 6.7 在類中實現嵌套的 foreach 功能