Chapter 3. Language and API Gotchas

Languages like C# and VB.NET allow you to develop object-oriented code without the complexity of syntax found in languages like C++. Nevertheless, there are features of the C# and VB.NET languages, and the .NET Framework types, that are likely to run counter to your intuitions. In particular, your experience with other languages may lead you to expect that certain features will behave in familiar ways in .NET and that the .NET languages will be consistent in how they handle common tasks. If so, you’ll be in for some surprises. For example, .NET takes a different approach to copy constructors than C++ does. The .NET XML reader needs some help to work as quickly as you might expect. And the VB.NET and C# language compilers don’t handle object initialization in the same way; nor do they spot bad enumerations at compile time, as you might expect.

In this chapter I focus on gotchas at the language and API level of the .NET platform.

GOTCHA #20 Singleton isn’t guaranteed process-wide

A static/Shared field belongs to the class and is not part of any instance. Typically, when you see a static/Shared field, you know that no matter how many instances of the class exist, there is one and only one occurrence of this field. Often a static/Shared field is used to limit the number of instances of an object—the concept of Singleton. (Refer to “Exploring the Singleton Design Pattern” and "Implementing the Singleton Pattern in C#" in the section "on the web" in the ...

Get .NET Gotchas now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.