Thinking in C# ( ) Revision 0.1 Bruce Eckel, President, MindView, Inc. Planet PDF brings you the Portable Document Format (PDF) version of Thinking in C# (Revision 0.1). Planet PDF is the premier PDF-related site on the web. There is news, software, white papers, interviews, product reviews, Web links, code samples, a forum, and regular articles by many of the most prominent and respected PDF experts in the world. Visit our sites for more detail: http://www.planetpdf.com/ http://www.pdfstore.com/ http://www.binarything.com/ 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 Overview Introduction 3 1: Those Who Can, Code 15 2: Introduction to Objects 15 3: Hello, Objects 51 4: Controlling Program Flow89 5: Initialization & Cleanup151 6: Coupling and Cohesion215 6a: Hiding the Implementation 234 7: Reusing classes 250 8: Interfaces and Implementation 295 8a: Interfaces 333 9: Collecting Your Objects391 10: Error Handling With Exceptions 483 11: I/O in C# 521 12: Reflection and Attributes559 13: Programming Windows Forms 589 14: GDI+ Overview 707 14: Multithreaded Programming 713 15: XML 751 16: Web Services 753 A: C# For Java Programmers 755 B: C# For Visual Basic Programmers 757 C: C# Programming Guidelines 759 D: Resources 771 Index 771 What’s Inside Interchangeable objects Introduction 3 with polymorphism...........31 Prerequisites........................3 Abstract base classes and interfaces35 Learning C#.........................3 Object landscapes and Goals....................................4 lifetimes.............................36 Online documentation........6 Collections and iterators..............37 Chapters...............................6 The singly rooted hierarchy..........39 Exercises..............................9 Collection libraries and support for Source code........................10 easy collection use.......................40 Coding standards..........................12 The housekeeping dilemma: who C# versions........................12 should clean up?...........................41 Seminars and mentoring..12 Exception handling: dealing Errors.................................12 with errors.........................43 Note on the cover design...13 Multithreading..................44 Acknowledgements...........13 Persistence.........................45 Internet contributors....................13 C# and the Internet...........45 1: Those Who Can, Code 15 What is the Web?..........................46 Client-side programming.............46 2: Introduction to Objects 15 Server-side programming............46 The progress of abstraction16 A separate arena: applications.....46 An object has an interface.19 Analysis and design...........47 An object provides services22 Extreme programming......47 The hidden implementation22 Why .NET succeeds...........47 Reusing the implementation24 Systems are easier to express and Inheritance: reusing the understand....................................47 interface.............................25 Maximal leverage with libraries...47 Is-a vs. is-like-a relationships......29 Error handling..............................47 Programming in the large............47 Strategies for transition....48 The Common Language Runtime77 Guidelines....................................48 Comments and embedded Management obstacles................50 documentation..................81 C# vs. Java?.......................50 Documentation Comments..........82 Summary...........................50 Documentation example..............85 Coding style.......................86 3: Hello, Objects 51 Summary...........................87 You manipulate objects with Exercises............................87 references..........................51 You must create all the 4: Controlling Program Flow objects................................52 89 Where storage lives......................53 Using C#operators............89 Arrays in Java..............................54 Precedence...................................90 Special case: value types..............55 Assignment..................................90 You never need to destroy C#’s Preprocessor........................115 an object............................56 foreach.............................135 Scoping.........................................56 5: Initialization & Cleanup Scope of objects.............................57 151 Creating new data types: class....................................58 Guaranteed initialization with the constructor.........151 Fields, Properties, and methods..59 Methods, arguments, and Method overloading........154 return values......................61 Distinguishing overloaded methods156 The argument list.........................62 Overloading with primitives.......157 Attributes and Meta- Overloading on return values.....162 Behavior.............................64 Default constructors...................162 Delegates...........................64 The this keyword.......................163 Properties..........................65 Cleanup: finalization and Creating New Value Types67 garbage collection............169 Enumerations...............................67 What are destructors for?............171 Structs..........................................68 Instead of a destructor, use Close() Building a C# program......69 or Dispose()................................172 Name visibility.............................69 Destructors, Dispose(), and the Using other components..............70 using keyword..............................177 The static keyword.......................71 The death condition...................182 Putting It All Together......73 How a garbage collector works..183 Member initialization......185 Compiling and running................76 Fine-tuning Compilation..............77 Specifying initialization..............187 Constructor initialization...........188 Array initialization..........195 Interface and Multidimensional arrays............200 implementation...............245 Sidebar/Appendix: What a Class access......................246 difference a rectangle makes.....203 Summary.........................249 Summary.........................213 Exercises..........................250 Exercises..........................214 7: Reusing classes 250 6: Coupling and Cohesion Composition syntax.........251 215 Inheritance syntax...........255 Software As Architecture vs. Initializing the base class...........258 Software Architecture.....217 Combining composition and What Is Software inheritance.......................261 Architecture.....................219 Guaranteeing proper cleanup....263 Simulation Architectures: Choosing composition vs. Always Taught, Rarely Used219 inheritance.......................267 Client/Server and n-Tier protected..........................269 Architectures...................220 Incremental development270 Layered Architectures.....222 Upcasting.........................271 Problem-Solving Why “upcasting”?.......................272 Architectures...................223 Explicit Overloading Only..........273 Dispatching Architectures223 The const and readonly “Not Really Object-Oriented”224 keywords..........................286 Design Is As Design Does224 Sealed classes..............................289 First, Do No Harm..........225 Emphasize virtual functions.....290 Design Rule #1: Write Initialization and class Boring Code.....................226 loading.............................291 Design Is As Design Does234 Initialization with inheritance....291 Summary.........................293 6a: Hiding the Exercises..........................294 Implementation 234 8: Interfaces and The namespace unit........235 Implementation 295 Creating unique package names236 Upcasting revisited..........297 Using imports to change behavior239 C#’s access specifiers......240 Forgetting the object type..........299 The twist..........................301 “Friendly”...................................240 public: interface access.............241 Method-call binding...................301 private: you can’t touch that!...242 Producing the right behavior.....303 protected: “sort of friendly”....244 Extensibility...............................306 Overriding vs. overloading310 Operator Overloading......311 Inner classes & control frameworks379 Abstract classes and Summary.........................387 methods............................311 Exercises.........................388 Constructors and 9: Collecting Your Objects polymorphism.................317 391 Order of constructor calls...........317 Arrays...............................391 Behavior of polymorphic methods Arrays are first-class objects......393 inside constructors.....................320 Designing with inheritance322 The Array class..........................398 Array’s Static Methods...............399 Pure inheritance vs. extension...324 Array element comparisons......402 Downcasting and run-time type What? No bubbles?...................404 identification..............................326 Summary.........................331 Unsafe Arrays............................406 Exercises..........................331 Get things right….......................409 … Then Get Them Fast...............413 8a: Interfaces 333 Array summary..........................420 Interfaces.........................333 Introduction to data “Multiple inheritance” in Java...338 structures........................420 Extending an interface with Queues and Stacks..........421 inheritance.................................342 ArrayList..........................424 Doesn’t work in C#. Must have BitArray...........................426 section on enums and structs earlier343 Dictionaries.....................428 Initializing fields in interfaces...346 Hashtable....................................428 Nesting interfaces......................347 ListDictionary.............................431 Inner classes....................350 SortedList...................................432 Inner classes and upcasting.......352 String specialists.........................433 Inner classes in methods and One Key, Multiple Values...........433 scopes.........................................355 Customizing Hashcode Providers434 Anonymous inner classes...........357 String specialists: The link to the outer class...........361 StringCollection and static inner classes....................364 StringDictionary..............436 Referring to the outer class object366 Container disadvantage: Reaching outward from a multiply- unknown type..................437 nested class................................368 Using CollectionBase to make Inheriting from inner classes.....369 type-conscious collections.........440 Can inner classes be overridden?370 IEnumerators..................442 Inner class identifiers................372 Custom Indexers.............444 Why inner classes?.....................373
Description: