Cover image for .NET Gotchas

Book description

Like most complex tasks, .NET programming is fraught with potential costly, and time-consuming hazards. The millions of Microsoft developers worldwide who create applications for the .NET platform can attest to that. Thankfully there's now a book that shows you how to avoid such costly and time-consuming mistakes. It's called .NET Gotchas. The ultimate guide for efficient, pain-free coding, .NET Gotchas from O'Reilly contains 75 common .NET programming pitfalls--and advice on how to work around them. It will help you steer away from those mistakes that cause application performance problems, or so taint code that it just doesn't work right. The book is organized into nine chapters, each focusing on those features and constructs of the .NET platform that consistently baffle developers. Within each chapter are several "gotchas," with detailed examples, discussions, and guidelines for avoiding them. No doubt about it, when applied, these concise presentations of best practices will help you lead a more productive, stress-free existence. What's more, because code examples are written in both VB.NET and C#, .NET Gotchas is of interest to more than 75 percent of the growing numbers of .NET programmers. So if you're a .NET developer who's mired in the trenches and yearning for a better way, this book is most definitely for you.

Table of Contents

  1. .NET Gotchas
  2. Dedication
  3. A Note Regarding Supplemental Files
  4. Preface
    1. Who This Book Is For
      1. How This Book Is Organized
        1. CLR/FRAMEWORK GOTCHAS
        2. VISUAL STUDIO AND COMPILER GOTCHAS
        3. LANGUAGE AND API GOTCHAS
        4. LANGUAGE INTEROPERABILITY GOTCHAS
        5. GARBAGE COLLECTION GOTCHAS
        6. INHERITANCE AND POLYMORPHISM GOTCHAS
        7. MULTITHREADING GOTCHAS
        8. COM-INTEROP AND ENTERPRISE SERVICES GOTCHAS
    2. What You Need to Use This Book
    3. Measure of Performance
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Comments and Questions
    7. Safari® Enabled
    8. Acknowledgments
  5. 1. CLR/Framework Gotchas
    1. GOTCHA #1 Type alias size doesn’t match what you’re familiar with
      1. IN A NUTSHELL
      2. SEE ALSO
    2. GOTCHA #2 struct and class differ in behavior
      1. IN A NUTSHELL
      2. SEE ALSO
    3. GOTCHA #3 Returning value types from a method/property is risky
      1. IN A NUTSHELL
      2. SEE ALSO
    4. GOTCHA #4 You can’t force calls to your value-type constructors
      1. IN A NUTSHELL
      2. SEE ALSO
    5. GOTCHA #5 String concatenation is expensive
      1. IN A NUTSHELL
      2. SEE ALSO
    6. GOTCHA #6 Exceptions may go unhandled
      1. IN A NUTSHELL
      2. SEE ALSO
    7. GOTCHA #7 Uninitialized event handlers aren’t treated gracefully
      1. IN A NUTSHELL
      2. SEE ALSO
    8. GOTCHA #8 Division operation isn’t consistent between types
      1. IN A NUTSHELL
      2. SEE ALSO
    9. GOTCHA #9 Typeless ArrayList isn’t type-safe
      1. IN A NUTSHELL
      2. SEE ALSO
    10. GOTCHA #10 Type.GetType() may not locate all types
      1. IN A NUTSHELL
      2. SEE ALSO
    11. GOTCHA #11 Public key reported by sn.exe is inconsistent
      1. IN A NUTSHELL
      2. SEE ALSO
  6. 2. Visual Studio and Compiler Gotchas
    1. GOTCHA #12 Compiler warnings may not be benign
      1. IN A NUTSHELL
      2. SEE ALSO
    2. GOTCHA #13 Ordering of catch processing isn’t consist across languages
      1. IN A NUTSHELL
      2. SEE ALSO
    3. GOTCHA #14 Type.GetType() might fail at run-time
      1. IN A NUTSHELL
      2. SEE ALSO
    4. GOTCHA #15 rethrow isn’t consistent
      1. IN A NUTSHELL
      2. SEE ALSO
    5. GOTCHA #16 Default of Option Strict (off) isn’t good
      1. IN A NUTSHELL
      2. SEE ALSO
    6. GOTCHA #17 Versioning may lead to Serialization headaches
      1. IN A NUTSHELL
      2. SEE ALSO
    7. GOTCHA #18 Creating Web apps can be painful
      1. IN A NUTSHELL
      2. SEE ALSO
    8. GOTCHA #19 Naming XML documentation for IntelliSense support isn’t intuitive
      1. IN A NUTSHELL
      2. SEE ALSO
  7. 3. Language and API Gotchas
    1. GOTCHA #20 Singleton isn’t guaranteed process-wide
      1. IN A NUTSHELL
      2. SEE ALSO
    2. GOTCHA #21 Default performance of Data.ReadXMLData.ReadXML
      1. IN A NUTSHELL
      2. SEE ALSO
    3. GOTCHA #22 enum lacks type-safety
      1. IN A NUTSHELL
      2. SEE ALSO
    4. GOTCHA #23 Copy Constructor hampers exensibility
      1. IN A NUTSHELL
      2. SEE ALSO
    5. GOTCHA #24 Clone() has limitations
      1. IN A NUTSHELL
      2. SEE ALSO
    6. GOTCHA #25 Access to static/Shared members isn’t enforced consistently
      1. IN A NUTSHELL
      2. SEE ALSO
    7. GOTCHA #26 Details of exception may be hidden
      1. IN A NUTSHELL
      2. SEE ALSO
    8. GOTCHA #27 Object initialization sequence isn’t consistent
      1. IN A NUTSHELL
      2. SEE ALSO
    9. GOTCHA #28 Polymorphism kicks in prematurely
      1. IN A NUTSHELL
      2. SEE ALSO
    10. GOTCHA #29 Unit testing private methodstesting private methods
      1. IN A NUTSHELL
      2. SEE ALSO
  8. 4. Language Interoperability Gotchas
    1. GOTCHA #30 Common Language Specification Compliance isn’t the default
      1. IN A NUTSHELL
      2. SEE ALSO
    2. GOTCHA #31 Optional parameters break interoperability
      1. IN A NUTSHELL
      2. SEE ALSO
    3. GOTCHA #32 Mixing case between class members breaks interoperability
      1. IN A NUTSHELL
      2. SEE ALSO
    4. GOTCHA #33 Name collision with keywords breaks interoperability
      1. IN A NUTSHELL
      2. SEE ALSO
    5. GOTCHA #34 Defining an array isn’t consistent
      1. IN A NUTSHELL
      2. SEE ALSO
  9. 5. Garbage Collection Gotchas
    1. GOTCHA #35 Writing Finalize() is rarely a good idea
      1. IN A NUTSHELL
      2. SEE ALSO
    2. GOTCHA #36 Releasing managed resources in Finalize() can wreak havoc
      1. IN A NUTSHELL
      2. SEE ALSO
    3. GOTCHA #37 Rules to invoke base.Finalize() aren’t consistent
      1. IN A NUTSHELL
      2. SEE ALSO
    4. GOTCHA #38 Depending on Finalize() can tie up critical resources
      1. IN A NUTSHELL
      2. SEE ALSO
    5. GOTCHA #39 Using Finalize() on disposed objects is costly
      1. IN A NUTSHELL
      2. SEE ALSO
    6. GOTCHA #40 Implementing IDisposable isn’t enough
      1. IN A NUTSHELL
      2. SEE ALSO
    7. GOTCHA #41 Using the Dispose Design Pattern doesn’t guarantee cleanup
      1. IN A NUTSHELL
      2. SEE ALSO
  10. 6. Inheritance and Polymorphism Gotchas
    1. GOTCHA #42 Runtime Type Identification can hurt extensibility
      1. IN A NUTSHELL
      2. SEE ALSO
    2. GOTCHA #43 Using new/shadows causes “hideous hiding”
      1. IN A NUTSHELL
      2. SEE ALSO
    3. GOTCHA #44 Compilers are lenient toward forgotten override/overrides
      1. IN A NUTSHELL
      2. SEE ALSO
    4. GOTCHA #45 Compilers lean toward hiding virtual methods
      1. IN A NUTSHELL
      2. SEE ALSO
    5. GOTCHA #46 Exception handling can break polymorphism
      1. IN A NUTSHELL
      2. SEE ALSO
    6. GOTCHA #47 Signature mismatches can lead to method hiding
      1. IN A NUTSHELL
      2. SEE ALSO
  11. 7. Multithreading Gotchas
    1. GOTCHA #48 The Thread classThread class
      1. IN A NUTSHELL
      2. SEE ALSO
    2. GOTCHA #49 Foreground threads may prevent a program from terminating
      1. IN A NUTSHELL
      2. SEE ALSO
    3. GOTCHA #50 Background threads don’t terminate gracefully
      1. IN A NUTSHELL
      2. SEE ALSO
    4. GOTCHA #51 Interrupt () kicks in only when a thread is blocked
      1. IN A NUTSHELL
      2. SEE ALSO
    5. GOTCHA #52 ThreadAbortException—a hot potato
      1. IN A NUTSHELL
      2. SEE ALSO
    6. GOTCHA #53 Environment.Exit() brings down the CLR
      1. IN A NUTSHELL
      2. SEE ALSO
    7. GOTCHA #54 ResetAbort() may lead to surprises
      1. IN A NUTSHELL
      2. SEE ALSO
    8. GOTCHA #55 Abort() takes time to clean up
      1. IN A NUTSHELL
      2. SEE ALSO
    9. GOTCHA #56 Calling Type.GetType() may not return what you expect
      1. IN A NUTSHELL
      2. SEE ALSO
    10. GOTCHA #57 Locking on globally visible objects is too sweeping
      1. IN A NUTSHELL
      2. SEE ALSO
    11. GOTCHA #58 Threads from the thread pool are scarce
      1. IN A NUTSHELL
      2. SEE ALSO
    12. GOTCHA #59 Threads invoked using delegates behave like background threads
      1. IN A NUTSHELL
      2. SEE ALSO
    13. GOTCHA #60 Passing parameters to threads is tricky
      1. IN A NUTSHELL
      2. SEE ALSO
    14. GOTCHA #61 Exceptions thrown from threads in the pool are lost
      1. IN A NUTSHELL
      2. SEE ALSO
    15. GOTCHA #62 Accessing WinForm controls from arbitrary threads is dangerous
      1. IN A NUTSHELL
      2. SEE ALSO
    16. GOTCHA #63 Web-service proxy may fail when used for multiple asynchronous calls
      1. IN A NUTSHELL
      2. SEE ALSO
    17. GOTCHA #64 Raising events lacks thread-safety
      1. IN A NUTSHELL
      2. SEE ALSO
  12. 8. COM-Interop and Enterprise Services Gotchas
    1. GOTCHA #65 Release of COM object is confusing
      1. IN A NUTSHELL
      2. SEE ALSO
    2. GOTCHA #66 Using interface pointers after calling ReleaseComObject() will fail
      1. IN A NUTSHELL
      2. SEE ALSO
    3. GOTCHA #67 Cross-apartment calls are expensive
      1. IN A NUTSHELL
      2. SEE ALSO
    4. GOTCHA #68 Default apartment of main thread is inconsistent across languages
      1. IN A NUTSHELL
      2. SEE ALSO
    5. GOTCHA #69 STAThread attribute may have no effect on your methods
      1. IN A NUTSHELL
      2. SEE ALSO
    6. GOTCHA #70 Spattering access to COM components makes code hard to maintain
      1. IN A NUTSHELL
      2. SEE ALSO
    7. GOTCHA #71 Auto-generating GUID for your classes leads to versioning woes
      1. IN A NUTSHELL
      2. SEE ALSO
    8. GOTCHA #72 All but one of the ClassInterface options are ineffective
      1. IN A NUTSHELL
      2. SEE ALSO
    9. GOTCHA #73 Simply tur ning the switch for COM interop is dangerous
      1. IN A NUTSHELL
      2. SEE ALSO
    10. GOTCHA #74 ServicedComponents implemented inconsistently on XP and 2003
      1. IN A NUTSHELL
      2. SEE ALSO
    11. GOTCHA #75 AutoComplete comes with undesirable side effects
      1. IN A NUTSHELL
      2. SEE ALSO
  13. A. Resources
    1. Bibliography
    2. On the Web
      1. Agile Developer
      2. CLR Profiler - Managed Code Performance Tool
      3. Common Object Operations
      4. Cruise Control .NET
      5. C# FAQs
      6. C# Language Specification
      7. Design Guidelines for Class Library Developers
      8. Don’t Lock Type Objects!
      9. Draco.NET
      10. ECMA-CLI
      11. Equals vs. ==
      12. Exploring the Singleton Design Pattern
      13. FxCop
      14. Garbage Collection
      15. Groups @ Google
      16. Gotdotnet
      17. Hashcode
      18. Implementing Finalize and Dispose to Clean Up Unmanaged Resources
      19. Implementing the Singleton Pattern in C#
      20. Languages Available in .NET
      21. MSDN
      22. MSDN Magazine
      23. NAnt
      24. NDoc
      25. NUnit
      26. PInvoke.net
      27. Reflector
      28. ReleaseComObject() Issues
      29. Test Driven Development
      30. Unexpected Errors in Managed Applications
      31. VB FAQs
      32. VS.NET Tips and Tricks
  14. Index
  15. About the Author
  16. Colophon
  17. Copyright