Thinking in C# phần 1 potx

81 341 0
Thinking in C# phần 1 potx

Đ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

Thinking in C# Larry O’Brien and Bruce Eckel Thinking in C# Larry O’Brien and Bruce Eckel Prentice Hall Upper Saddle River, New Jersey 07458 www.phptr.com Dedication For Tina, who makes me happy — LOB Overview Preface: Computer Language 1 1: Those Who Can, Code 1 2: Introduction to Objects 19 3: Hello, Objects 49 4: Controlling Program Flow 87 5: Initialization and Cleanup 149 6: Hiding the Implementation 199 7: Reusing Classes 219 8: Interfaces and Implementation 261 9: Coupling and Cohesion 315 10: Collecting Your Objects 349 11: Error Handling with Exceptions 439 12: I/O in C# 473 13: Reflection and Attributes 511 14: Programming Windows Forms 547 15: GDI+ Overview 659 16: Multithreaded Programming 711 17: XML 773 18: Web Programming 824 A: C# for Visual Basic Programmers 879 B: C# for Java Programmers 883 C: Test-First Programming with NUnit 887 D: Programming the Pocket PC 895 E: C# programming guidelines 903 F: Resources 915 Concordance 917 Class, Method, Property Cross-Reference 921 Index 929 What’s Inside Preface: Computer Language 1 Prerequisites 2 Learning C# 3 Goals 4 Online documentation 5 Exercises 5 Source code 5 Coding standards 7 C# and .NET versions 8 Seminars and mentoring 8 Errors 8 Note on the cover design 8 Acknowledgments 9 1: Those Who Can, Code 1 Software economics 3 C# and the Internet 4 Static sites 5 Dynamic sites 5 Peer-to-peer 5 Web services 6 Security 7 Analysis, design, and Extreme Programming 7 Strategies for transition 9 Management obstacles 10 Return on investment 11 Summary 17 2: Introduction to Objects 19 The progress of abstraction 19 An object has an interface 22 The hidden implementation 24 Reusing the implementation . 26 Inheritance: Reusing the interface 27 Is-a vs. is-like-a relationships 31 Interchangeable objects with polymorphism 32 Abstract base classes and interfaces37 Object landscapes and lifetimes37 Collections and iterators 38 The singly rooted hierarchy 40 Collection libraries and support for easy collection use 41 The housekeeping dilemma: who should clean up? 42 Exception handling: dealing with errors 43 Multithreading 44 Persistence 45 Remote objects 46 Summary 46 3: Hello, Objects You manipulate objects with references 49 You must create all the objects50 Where storage lives 50 Arrays in C# 52 Special case: value types 52 You never need to destroy an object 53 Scoping 53 Scope of objects 54 Creating new data types: class55 Fields, properties, and methods 55 Methods, arguments, and return values 58 The argument list 59 Attributes and meta-behavior60 Delegates 61 Properties 62 Creating new value types 63 Enumerations 64 Structs 64 Building a C# program 65 Name visibility 67 Using other components 69 The static keyword 70 Putting it all together 71 Compiling and running 73 Fine-tuning compilation 75 The Common Language Runtime.75 Comments and embedded documentation 79 Documentation Comments 80 Documentation example 82 Coding style 84 Summary 85 Exercises 85 4: Controlling Program Flow 87 Using C#operators 87 Precedence 87 Assignment 88 Aliasing during method calls 89 Aliasing and object state 90 Aliasing and the ref keyword 92 Beyond aliasing with out 95 Mathematical operators 98 Unary minus and plus operators100 Auto increment and decrement. 100 Relational operators 101 Testing object equivalence 102 Logical operators 103 Short-circuiting 104 Bitwise operators 105 Shift operators 106 Ternary if-else operator 111 The comma operator 112 Common pitfalls when using operators 113 Casting operators 113 Literals 114 Promotion 116 C# has sizeof 116 C#’s preprocessor 116 Precedence revisited 118 A compendium of operators 118 Execution control 130 true and false 130 if-else 130 return 131 Iteration 132 do-while 133 for 133 foreach 135 The comma operator 136 break and continue 136 The infamous goto 138 switch 144 Summary 146 Exercises 146 5: Initialization and Cleanup Guaranteed initialization with the constructor 149 Method overloading 152 Distinguishing overloaded methods154 Overloading with primitives 155 Overloading on return values 159 Default constructors 159 The this keyword 160 Calling constructors from constructors 164 The meaning of static 165 Cleanup: finalization and garbage collection 166 What are destructors for? 167 Instead of a destructor, implement IDisposable.Dispose( ) 168 Destructors, IDisposable, and the using keyword 173 How a garbage collector works 174 Member initialization 176 Specifying initialization 178 Constructor initialization 179 Array initialization 185 The params method modifier 189 Multidimensional arrays 190 What a difference a rectangle makes194 Summary 195 Exercises 196 6: Hiding the Implementation 199 Organizing with namespaces200 Creating unique names 202 Using #define to change behavior204 C#’s access specifiers 206 public: interface access 206 internal 207 private: you can’t touch that! 208 protected 210 Interface and implementation211 Class access 213 Summary 215 Exercises 217 7: Reusing Classes 219 Composition syntax 220 Inheritance syntax 223 Initializing the base class 225 Combining composition and inheritance 229 Guaranteeing proper cleanup 230 Choosing composition vs. inheritance 234 protected 235 Incremental development 236 Upcasting 237 Why “upcasting”? 238 Explicit overloading only 239 The const and readonly keywords 251 Sealed classes 253 Emphasize virtual functions 254 Initialization and class loading255 Initialization with inheritance 255 Summary 257 Exercises 258 8: Interfaces and Implementation Upcasting revisited 262 Forgetting the object type 263 The twist 265 Method-call binding 265 Producing the right behavior 266 Extensibility 270 Static methods cannot be virtual 274 Overriding vs. overloading 276 Operator overloading 278 Multiargument operator overloading 282 Explicit and implicit type conversions 283 Operator overloading design guidelines 285 Abstract classes and methods285 Constructors and polymorphism 289 Order of constructor calls 290 Behavior of polymorphic methods inside constructors 292 Designing with inheritance .294 Pure inheritance vs. extension 295 Downcasting and run-time type identification 298 Interfaces 302 “Multiple inheritance” in C# 305 Extending an interface with inheritance 309 Summary 310 Exercises 311 9: Coupling and Cohesion 315 Software as architecture vs. software architecture 315 What is software architecture?317 Simulation architectures: always taught, rarely used 318 Client/server and n-tier architectures 318 Layered architectures 322 Problem-solving architectures323 Dispatching architectures 323 “Not really object-oriented” 324 Coupling 324 Cohesion 329 Design is as design does 339 First, do no harm 339 Write boring code 340 Make names meaningful 340 Limit complexity 341 Make stuff as private as possible 343 Coupling, cohesion, and design trends 344 Summary 345 Exercises 346 10: Collecting Your Objects 349 Arrays 349 Arrays are first-class objects 351 The Array class 355 Array’s static methods 355 Array element comparisons 358 What? No bubbles? 360 Unsafe arrays 362 Get things right… 366 … then get them fast 368 Array summary 375 Introduction to data structures380 Queues and stacks 381 ArrayList 384 BitArray 386 Dictionaries 387 Hashtable 388 ListDictionary 391 SortedList 391 String specialists 392 One key, multiple values 392 Customizing hashcode providers394 String specialists: StringCollection and StringDictionary 396 Container disadvantage: unknown type 397 Using CollectionBase to make type-conscious collections 399 IEnumerators 401 Custom indexers 403 Custom enumerators & data structures 406 Sorting and searching Lists413 From collections to arrays 414 Persistent data with ADO.NET421 Getting a handle on data with DataSet 422 Connecting to a database 425 Fast reading with IDataReader428 CRUD with ADO.NET 430 Update and delete 430 The object-relational impedance mismatch 434 Summary 435 Exercises 436 11: Error Handling with Exceptions Basic exceptions 441 Exception arguments 442 Catching an exception 443 [...]... http://www.ThinkingIn.Net/tools.html 2 Thinking in C# www.ThinkingIn.NET increasing) productivity Java programmers sick of the complexity of J2EE or frustrated by the lack of OS integration will be thrilled by the productivity of the NET Framework while Visual Basic programmers have in C# the ideal opportunity to move into the mainstream of languages derived from the C syntax C# could even be a good stepping... control 615 TreeView and ListView 616 ListView 618 Bitmaps 692 Rich clients with interop .698 COM Interop and the WebBrowser control 698 COM Interop challenges 7 01 Non-COM Interop 702 Summary 707 Exercises 708 16 : Multithreaded Programming Responsive user interfaces 711 NET’s threading model 714 Running a thread 716 Icon views 618 Waiting for a thread to complete 717 Details... from the 1st edition of the book "Thinking in C#. " All rights reserved EXCEPT as allowed by the following statements: You can freely use this file for your own work (personal or commercial), including modifications and distribution in executable form only Permission is granted to use this file in classroom situations, including its use in presentation materials, as long as the book "Thinking in C#" is... the diveboat FeBrina for reminding me that technology is just a way to get to the good stuff x Thinking in C# www.ThinkingIn.NET 1: Those Who Can, Code Computer programming is tremendous fun Like music, it is a skill that derives from an unknown blend of innate talent and constant practice Like drawing, it can be shaped to a variety of ends – commercial, artistic, and pure entertainment Programmers... any kind, including any implied warranty of merchantability, fitness for a particular purpose or non-infringement The entire risk as to the quality and performance of the software is with you Larry O’Brien, Bruce Eckel, and the publisher shall not be liable for any damages suffered by you or any third party as a result of using or distributing software In no event will vi Thinking in C# www.ThinkingIn.NET... Constructors 464 Exception matching 468 Exception guidelines 469 Summary 470 Exercises 470 12 : I/O in C# File, Directory, and Path 473 A directory lister 473 Checking for and creating 13 : Reflection and Attributes The need for RTTI 511 The Type object 514 Checking before a cast 517 RTTI syntax 523 Reflection: run-time class information 525 Adding meta-information with attributes... Checking capitalization style 504 Supertype matching 444 Summary 508 Exercises 508 Exceptions have a helplink 444 Creating your own exceptions445 C# s lack of checked exceptions4 51 Catching any exception 452 Rethrowing an exception 453 Elevating the abstraction level 453 Standard C# exceptions 455 Performing cleanup with finally 456 What’s finally for? 457 Finally and using .4 61. .. correction If you think you've found an error, please submit the correction using the form you will find at www.ThinkingIn.Net (Please use the same form for non-code errors found in the book.) ///:~ You may use the code in your projects and in the classroom (including your presentation materials) as long as the copyright notice that appears in each source file is retained Coding standards In the text of... recently available to humanity People viii Thinking in C# www.MindView.net who just use computers to send email, play videogames, and surf the Internet are missing their opportunity to actively participate in the opening of a great intellectual frontier Acknowledgments (by Larry O’Brien) First, I have to thank Bruce Eckel for entrusting me to work with the Thinking In structure Without this proven framework,... navigation 8 01 An XPath explorer 807 Transforming a document 815 Summary 8 21 Exercises 822 18 : Web Programming Identifying a machine 824 Sockets 826 Whois for ZoneAlarm 826 Receiving incoming connections833 Modifying XML returns .874 Summary 876 Exercises 877 A: C# for Visual Basic Programmers B: C# for Java Programmers C: Test-First Programming with NUnit D: Programming the Pocket . operators 10 6 Ternary if-else operator 11 1 The comma operator 11 2 Common pitfalls when using operators 11 3 Casting operators 11 3 Literals 11 4 Promotion 11 6 C# has sizeof 11 6 C# s preprocessor. 12 : I/O in C# 473 13 : Reflection and Attributes 511 14 : Programming Windows Forms 547 15 : GDI+ Overview 659 16 : Multithreaded Programming 711 17 : XML 773 18 : Web Programming 824 A: C#. preprocessor 11 6 Precedence revisited 11 8 A compendium of operators 11 8 Execution control 13 0 true and false 13 0 if-else 13 0 return 13 1 Iteration 13 2 do-while 13 3 for 13 3 foreach 13 5 The

Ngày đăng: 06/08/2014, 20:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan