1. Trang chủ
  2. » Công Nghệ Thông Tin

visual c# 2010 recipes (apress)

1K 3,7K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 1.017
Dung lượng 9,26 MB

Nội dung

Tài liệu học lập trình C# cho sinh viên và mọi người.

Jones Freeman Visual Companion eBook Available THE EXPERT’S VOICE ® IN C# Visual C# 2010 Recipes A Problem-Solution Approach Allen Jones and Adam Freeman Quick answers and ready-to-use code to get you more productive in C# 2010 development 7.5 x 9.25 spine = 1.90625" 1016 page countthis print for content only—size & color not accurate BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® CYAN MAGENTA YELLOW BLACK US $54.99 Shelve in: .NET User level: Intermediate–Advanced www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2525-6 9 781430 225256 5 54 9 9 Visual C# 2010 Recipes: A Problem-Solution Approach Dear Reader, Whatever your situation, whatever the circumstance, one thing is certain: you will always know what you want to do, but not necessarily how best to do it. C# 2010 development is no exception, and this book is an invaluable companion when you are tackling a wide range of C# subjects: • Application domains, reflection, and metadata • Tasks, Threads, processes, and synchronization • Files, directories, and I/O • LINQ • XML processing • Windows Forms and Windows Presentation Foundation applications • Database access using ADO.NET • Networking and remoting • Security and cryptography • Unmanaged code interoperability • Commonly used interfaces and patterns • Windows integration This book shows you how to solve the types of development problems you will face every day. It contains hundreds of recipes, presented in a concise problem/ solution format so that you can find the answer to your question fast and get on with your development. Each recipe provides working code that demon- strates the solution, as well as additional information to give you a more in-depth understanding of the classes and techniques used to solve the problem. Allen Jones & Adam Freeman Allen Jones, author of WPF Recipes in C# 2010 Professional C# 2005 C# Programmers Cookbook Programming .NET Security Microsoft .NET XML Web Services Step by Step C# for Java Developers Adam Freeman, author of Programming .NET Security Microsoft .NET XML Web Services Step by Step C# for Java Developers THE APRESS ROADMAP Pro WPF in C# 2010 Pro LINQ in C# 2010 Accelerated C# 2010 Pro C# 2010 and the .NET 4.0 Platform Visual C# 2010 Recipes C# 2010 Recipes Visual C# 2010 Recipes A Problem-Solution Approach ■ ■ ■ Allen Jones and Adam Freeman Visual C# 2010 Recipes: A Problem-Solution Approach Copyright © 2010 by Allen Jones and Adam Freeman All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2525-6 ISBN-13 (electronic): 978-1-4302-2526-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Publisher and President: Paul Manning Lead Editor: Jonathan Hassell Technical Reviewer: Mark Collins Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anne Collett Copy Editor: Damon Larson Production Support: Patrick Cunningham Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. For my lovely wife Lena, and our three wonderful girls, Anya, Alexia, and Angelina. I love you all. —Allen Jones For my wife, Jacqui Griffyth, who I love a great deal. —Adam Freeman iv Contents at a Glance About the Author xx About the Technical Reviewers xxi Acknowledgments xxii Introduction xxiii ■Chapter 1: Application Development 1 ■Chapter 2: Data Manipulation 53 ■Chapter 3: Application Domains, Reflection, and Metadata 103 ■Chapter 4: Threads, Processes, and Synchronization 149 ■Chapter 5: Files, Directories, and I/O 205 ■Chapter 6: XML Processing 261 ■Chapter 7: Windows Forms 307 ■Chapter 8: Graphics, Multimedia, and Printing 369 ■Chapter 9: Database Access 423 ■Chapter 10: Networking 479 ■Chapter 11: Security and Cryptography 543 ■Chapter 12: Unmanaged Code Interoperability 597 ■ CONTENTS AT A GLANCE v ■Chapter 13: Commonly Used Interfaces and Patterns 619 ■Chapter 14: Windows Integration 683 ■Chapter 15: Parallel Programming 729 ■Chapter 16: Using LINQ 749 ■Chapter 17: Windows Presentation Foundation 789 Index 905 vi Contents About the Author xx About the Technical Reviewers xxi Acknowledgments xxii Introduction xxiii ■Chapter 1: Application Development 1 1-1. Create a Console Application from the Command Line 2 1-2. Create a Windows-Based Application from the Command Line 5 1-3. Create and Use a Code Module 9 1-4. Create and Use a Code Library from the Command Line 11 1-5. Access Command-Line Arguments 12 1-6. Include Code Selectively at Build Time 14 1-7. Access a Program Element That Has the Same Name As a Keyword 18 1-8. Create and Manage Strongly Named Key Pairs 19 1-9. Give an Assembly a Strong Name 21 1-10. Verify That a Strongly Named Assembly Has Not Been Modified 23 1-11. Delay-Sign an Assembly 24 1-12. Sign an Assembly with an Authenticode Digital Signature 26 1-13. Create and Trust a Test Software Publisher Certificate 28 1-14. Manage the Global Assembly Cache 29 1-15. Prevent People from Decompiling Your Code 30 ■ CONTENTS vii 1-16. Manipulate the Appearance of the Console 31 1-17. Create a Static Class 34 1-18. Create an Anonymous Type 35 1-19. Create an ExpandoObject Dynamic Type 37 1-20. Define an Automatically Implemented Property 39 1-21. Overload an Operator 41 1-22. Define a Conversion Operator 44 1-23. Handle an Event with an Anonymous Function 46 1-24. Implement a Custom Indexer 48 ■Chapter 2: Data Manipulation 53 2-1. Manipulate the Contents of a String Efficiently 54 2-2. Encode a String Using Alternate Character Encoding 56 2-3. Convert Basic Value Types to Byte Arrays 59 2-4. Base64 Encode Binary Data 61 2-5. Validate Input Using Regular Expressions 65 2-6. Use Compiled Regular Expressions 70 2-7. Create Dates and Times from Strings 72 2-8. Add, Subtract, and Compare Dates and Times 74 2-9. Sort an Array or a Collection 77 2-10. Copy a Collection to an Array 79 2-11. Use a Strongly Typed Collection 81 2-12. Create a Generic Type 83 2-13. Store a Serializable Object to a File 87 2-14. Serialize an Object Using JSON 90 2-15. Read User Input from the Console 93 ■ CONTENTS viii 2-16. Using Large Integer Values 96 2-17. Select Collection or Array Elements 97 2-18. Remove Duplicate Items from an Array or Collection 100 ■Chapter 3: Application Domains, Reflection, and Metadata 103 3-1. Create an Application Domain 104 3-2. Create Types That Can Be Passed Across Application Domain Boundaries 106 3-3. Avoid Loading Unnecessary Assemblies into Application Domains 109 3-4. Create a Type That Cannot Cross Application Domain Boundaries 111 3-5. Load an Assembly into the Current Application Domain 111 3-6. Execute an Assembly in a Different Application Domain 114 3-7. Instantiate a Type in a Different Application Domain 116 3-8. Pass Data Between Application Domains 121 3-9. Unload Assemblies and Application Domains 124 3-10. Retrieve Type Information 125 3-11. Test an Object’s Type 127 3-12. Instantiate an Object Using Reflection 129 3-13. Create a Custom Attribute 133 3-14. Inspect the Attributes of a Program Element Using Reflection 136 3-15. Programmatically Discover the Members of a Type 137 3-16. Invoke a Type Member Using Reflection 140 3-17. Dynamically Invoke a Type Member 142 3-18. Create a Custom Dynamic Type 143 ■Chapter 4: Threads, Processes, and Synchronization 149 4-1. Execute a Method Using the Thread Pool 151 4-2. Execute a Method Asynchronously 154 [...]... ReadString("Please enter your name : "); // Welcome the reader to Visual C# 2010 Recipes WriteString("Welcome to Visual C# 2010 Recipes, " + name); } } } The HelloWorld class listed next uses the ConsoleUtils class to display the message “Hello, world” to the console (HelloWorld is contained in the HelloWorld.cs file): using System; namespace Apress.VisualCSharpRecipes.Chapter01 { class HelloWorld { public static... box MessageBox.Show("Welcome to Visual C# 2010 Recipes, " + textBox1.Text, "Visual C# 2010 Recipes" ); } // Application entry point, creates an instance of the form, and begins // running a standard message loop on the current thread The message // loop feeds the application with input from the user as events [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new... without them Allen Jones Adam Freeman xxii Introduction Mastering the development of Microsoft NET Framework applications in C# is less about knowing the C# language and more about knowing how to use the functionality of the NET Framework class library most effectively Visual C# 2010 Recipes explores the breadth of the NET Framework class library and provides specific solutions to common and interesting... application that demonstrates the techniques just listed When run, it prompts a user to enter a name and then displays a message box welcoming the user to Visual C# 2010 Recipes using System; using System.Windows.Forms; namespace Apress.VisualCSharpRecipes.Chapter01 { class Recipe01_02 : Form { // Private members to hold references to the form's controls private Label label1; private TextBox textBox1;... presented in a succinct problem/solution format, and most are accompanied by working code samples Visual C# 2010 Recipes is not intended to teach you how to program, nor to teach you C# However, if you have even the most rudimentary experience programming applications built on the NET Framework using C#, you will find this book to be an invaluable resource Ideally, when you are facing a problem, this... covers some of the fundamental activities you will need to perform when developing your C# solutions The recipes in this chapter describe how to do the following: • Use the C# command-line compiler to build console and Windows Forms applications (recipes 1-1 and 1-2) • Create and use code modules and libraries (recipes 1-3 and 1-4) • Access command-line arguments from within your applications (recipe... the solution, or at least it will point you in the right direction Even if you just want to broaden your knowledge of the NET Framework class library, Visual C# 2010 Recipes is the perfect resource to assist you However, you cannot become proficient with C# and the classes in the NET Framework class library merely by reading about them Rather, you must use them and experiment with them by writing code,... controls this.ClientSize = new System.Drawing.Size(292, 126); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Name = "form1"; this.Text = "Visual C# 2010 Recipes" ; // Resume the layout logic of the form now that all controls are // configured this.ResumeLayout(false); } // Event handler called when the user clicks the Enter button on the // form... at build time (recipe 1-6) • Access program elements built in other languages whose names conflict with C# keywords (recipe 1-7) • Give assemblies strong names and verify strong-named assemblies (recipes 1-8, 1-9, 1-10, and 1-11) • Sign an assembly with a Microsoft Authenticode digital signature (recipes 1-12 and 1-13) • Manage the shared assemblies that are stored in the global assembly cache (recipe... HelloWorld.exe from the two source files, use the following command: csc /target:exe /main:Apress.VisualCSharpRecipes.Chapter01.HelloWorld /out:HelloWorld.exe ConsoleUtils.cs HelloWorld.cs 4 CHAPTER 1 ■ APPLICATION DEVELOPMENT 1-2 Create a Windows-Based Application from the Command Line Problem You need to use the C# command-line compiler to build an application that provides a Windows Forms– based GUI Solution

Ngày đăng: 24/01/2014, 19:46

TỪ KHÓA LIÊN QUAN