Chapter 13. Minor changes to simplify code

 

This chapter covers

  • Optional parameters
  • Named arguments
  • Streamlining ref parameters in COM
  • Embedding COM primary interop assemblies
  • Calling named indexers declared in COM
  • Generic variance for interfaces and delegates
  • Changes in locking and field-like events

Just as in previous versions, C# 4 has a few minor features that don’t merit individual chapters to themselves. In fact, there’s only one really big feature in C# 4—dynamic typing—which we’ll cover in the next chapter. The changes we’ll cover here just make C# that little bit more pleasant to work with, particularly if you work with COM on a regular basis. These features generally make code clearer, remove drudgery from COM calls, or simplify deployment.

Will any of those make your heart race with excitement? It’s unlikely. They’re nice features all the same, and some of them may be widely applicable. Let’s start by looking at how we call methods.

13.1. Optional parameters and named arguments

These are perhaps the Batman and Robin[1] features of C# 4. They’re distinct, but usually seen together. I’m going to keep them apart for the moment so we can examine each in turn, but then we’ll use them together for some more interesting examples.

1 Or Cavalleria Rusticana and Pagliacci if you’re feeling more highly cultured.

13.2. Improvements for COM interoperability

13.3. Generic variance for interfaces and delegates

13.4. Teeny tiny changes to locking and field-like events

13.5. Summary