C# in Depth

Cover of C# in Depth
Order now (3rd edition)

There's more to functional coding than functions as first class data

Chapter 13: Elegant code in the new era: 13.1.1

Created: 3/6/2008
Last updated: 3/6/2008

I mention the more functional emphasis of C# 3 a number of times in the book. There are various aspects to this: firstly there's the language features of making it simpler to create delegates, and possible to create expression trees.

However, there's also the library aspect - the way that the sequence operators never modify the existing data, for instance. Indeed, comparing List<T>.Sort and IEnumerable<T>.OrderBy shows this difference - sorting an existing collection is a mutating operation, whereas creating a new sequence based on an old one, but with a different order, is not.

Immutability is a core principle of functional languages, enabling simpler concurrency and in many cases making it easier to reason about how your program works. I hope it will be more strongly supported in C# 4.

In the meantime, C# 3 allows you to write functional-style code, and LINQ encourages you to do so - but don't fall into the trap of thinking it's just about lambda expressions. (I've personally got a long way to go when it comes to a functional style of programming, by the way, so don't think I'm looking down on anyone.)