Tuesday 19 October 2010

Book review: C# in depth second edition

c#indepth As the title of this post suggests this is the second edition of the popular
C# in Depth by the legend that is Jon Skeet published by Manning Books.  I never managed to read the first edition so this review will be from the perspective of a first time reader rather than any sort of  comparison.

The book is split into 4 parts plus appendices:

  • Part 1 - Preparing for the journey
  • Part 2 – C#2: solving the issues of C#1
  • Part 3 - C# 3: revolutionizing how we code
  • Part 4 - C# 4: playing nicely with others

This isn’t a short book at 493 pages of specific content and a further 30+ pages of appendices, I’ve read longer books but with the content being all technical it may well feel longer due to the detail.

Part 1- Preparing for the journey

This part is made up of 2 chapters, the first of which takes a breath taking dash through various language features and shows you how the language has evolved from version 1 through to version 4, Jon states that his intention is not to explain everything he mentions at this point but rather to give a flavour of how the language has changed over time.

Chapter 2 then takes us into delegates and the type system spending time illuminating us over how reference and value types really work.

Part 2 – C#2: solving the issues of C#1

With all the changes that have been made to C# over the years it isn’t until you start looking back at C# 1 that you remember the pain we developers put up with and how C# 2 was such a major change when it arrived.

The 5 chapters in this part cover topics ranging from the heavy weight changes such as generics and variance (co & contra) to the more lightweight like separate getter/setter properties and static classes.

It is also in this part that Jon first explains covariance and contravariance which to seems to be a subject that a lot of people (including myself) have struggled with, once Jon has explained it you may well say what I did “oh I know about that I just didn’t know it was called co and contra variance”

Part 3 - C# 3: revolutionizing how we code

So we get to part 3 and in the evolution of C# we reach version 3 and as Jon states in the introduction:

“Almost every feature in C# 3 enables one specific technology: LINQ.”

C# 2 brought many different improvements but they weren’t focused on any one thing whereas C#3 had many improvements which could be used independently but together are more than the sum of their parts.

Part 3 consists of 5 chapters just like part 2 with each chapter introducing a different feature and then showing how it builds on the previous to help provide the LINQ functionality.

The main focus is LINQ to objects but in the final chapter of this part, chapter 12, Jon explores subjects such as LINQ to Sql, LINQ to Xml, reactive extensions, etc he states he is not attempting to give in-depth coverage of these subjects but rather give the reader an idea of how far LINQ permeates and some of the things that you can do with it.

Part 4 - C# 4: playing nicely with others

By the time we reach part 4 we’ve covered, or at least mentioned, all the functionality that is available in C# 3 in a fair amount of depth and we move on to the new aspects of the language introduced with C# 4.

Part 4 has 4 chapters with the main content being in the first 2 and the second 2 are more speculative but I'll come back to that in a bit.

Chapters 13 covers a fair number of changes that have been made to the language such as named & optional parameters, generic variance & COM interoperability.  Again Jon shows the thought behind each of the changes and how they are useful separately but together they build to provide a larger set of functionality, an example of this shows Word automation in C#3 and the equivalent code in C#4 which takes half the code when utilising C# 4 functionality such as named parameters and embedded COM primary interop assemblies. 

Jon originally introduced variance to us back in ‘Part 2 – C#2: solving the issues of C#1’ explaining the support C# already had for it.  He now revisits variance but this time from a generic perspective explaining how co & contra variance has been added for generics to make it easier to use so you could now have a function return IShape and covariance would allow the function itself to return any type that implemented IShape which previously you wouldn’t have been able to do.

Chapter 14 then moves onto what could easily be considered the heavy weight change of C# 4 and the dynamic keyword.  Jon gives an overview of what dynamic typing is, how it is implemented within the framework through the dynamic keyword and then lifts the curtain to expose some of the inner workings with the ExpandoObject, DynamicObject and IDynamicMetaObjectProvider.  He also shows you how you can utilise the Dynamic Language Runtime showing us some examples of using Iron Python within a C# application to potentially provide end users with a way to modify the behaviour of an application through scripting.

As I mentioned above the final 2 chapters are a bit speculative covering subjects that are either brand new now or may come about in the near future.

In chapter 15 Jon discusses Code Contracts and his belief that in the next few years they will become part of normal development and that people will look back on code written before contracts and wonder how you managed without them.  Code contracts are a way to explicitly state the behaviour of your code effectively guarantying what it will or won’t return when you call it with the option for the premium & ultimate editions to enable this checking when building your code to effectively catch errors before you have even written a test for it.

Chapter 16 is the final chapter and in the introduction Jon says:

Rather than leave you with an abrupt context switch from Code Contracts to the appendixes,I wanted to wind down with a few thoughts about how far we’ve come and where we might be going

So rather than cover subjects with his normal technical depth Jon sums up the journey we’ve taken and does a little crystal ball gazing to give us the benefit of his experience as to what he believes is likely to be important in the near future.

Appendices

Once you’ve finished the 16 chapters if that isn’t enough you can keep going through the 3 appendices:

  • Appendix A covers LINQ standard query operators explaining what to use the operators for and providing examples of each.
  • Appendix B is all about generic collections in .Net listing the various collections and providing additional information and insight from Jon into the collection and its use.
  • Appendix C provides summaries of the different versions of the .Net framework, the C# language, the CLR and some related frameworks such as the compact framework.

Summary

I will admit that I wish I had read the first edition of this book 3 years ago, I am probably one of the “alpha geeks” that Jon mentions in his summary, I love to ‘look under the covers’ to learn a bit more information about how things work even if I may not get to use all of that knowledge.

It is interesting to see the evolution of C# through its various versions and it is only when its all brought together that you can see just how far and fast that it has evolved and one thing that does stand out is how important delegates are within .Net and how fundamental they are to LINQ and most likely future developments within the language.

Although Jon states that this book is aimed at a level suitable for junior developers I believe that even more experienced developers will gain something from reading this book even if its just Jon’s insight into some of the language features and its uses.

So would I recommend you buy this book? Yes, I believe you will find it invaluable.

 

My thanks to Manning books for allowing me to review this book and of course to Jon Skeet for updating his book, I’m already looking forward to reviewing the 3rd edition covering C# 5.

Saturday 9 October 2010

Developer! Developer! Developer! Ireland 10

So I had a great time presenting at DDDIE10 today and as a bonus it was recorded and will be up on channel 9 at some point in the near future.

As I promised the slides are available here.  If you attended my presentation at DDD SW in June you’ll find that I’ve changed the slides (and slide order) slightly to try and make it more understandable.

If you view the slides online the links on the resource page won’t work but if you download it then you should be able to get to the links.

As I said in my presentation if you want any more information feel free to get in touch.