Wednesday, November 28, 2007
In the past providing public "read only" access to a member would look something like:
With Automatic Properties in C# 3.5 the code becomes:
The neat trick is the access modifier (private) next to the set. This allow you to internally modify the member, while publicly providing "read only" access.
Monday, November 26, 2007
It's not a big change, so I'll use this blog entry to update my Xceed <-> Linq code. I've fixed some minor bugs, improved performance a lot and added better linq syntax support.
So if you're using the code, it'd be a good idea to update.
Monday, November 19, 2007
I was inspired by my good friend Nick's latest blog entry Return of the Batch Enumerable.
Here he demonstrates a neat idea of using extension methods and yield to split an IEnumerable into arbitrary batches.
My version is slightly less efficient, but treats the batches as separate lazy collections - so you can use it on multiple threads etc.
I've also added a Split extension, which I'll describe - but first, the code!
The InBatches returns the same output as Nicks:
And the Split batches the output across the collections:
So this gives you 2 different batch splitting methods for a generic enumerable.
I love this stuff.
Tuesday, November 13, 2007
Check the following code: We safely open a file (with the using statement), and yield the results with a loop.
This simple (and beautiful) code allows us to treat a file like an array of strings - which becomes very powerful when coupled with linq.
I'm sure this same pattern could also be applied to other data retrieval methods, eg algorithm calculation (prime numbers, Fibonacci, etc), network communication (message queue) and even as an abstraction over asynchronous data methods.
Yield provides a lazy enumeration (doesn't execute unless needed) so the solution is generic and efficient - I like it.
Monday, November 5, 2007
"How many ways can six people (Josh, Toby, CJ, Sam, Leo & Donna) sit, if Josh must sit to the left of Leo and to the right of Sam."
Now the astute reader will immediately recognize the answer to be 2 * (4! + 3!3!) = 120; but to be sure I wanted to double check this via C#. (I'm keen to redo this in F# too)
My solution was a quick hack, and is by no means "the best way" - but using linq it sure looks pretty.
This simple example shows why I'm a such fan of linq in C# - the pre-linq code for this certainly wouldn't be as eloquent.
Oh, and it also shows why I'm a fan of maths... it's much more efficient to just to calculate the factorials.
Update: Formatting code in a blog sure is a pain!