First encounters with Reactive Extensions

I’ve been researching Reactive Extensions for the last few days, with an eye to writing a short section in chapter 12 of the second edition of C# in Depth. (This is the most radically changed chapter from the first edition; it will be covering LINQ to SQL, IQueryable, LINQ to XML, Parallel LINQ, Reactive Extensions, and writing your own LINQ to Objects operators.) I’ve watched various videos from Channel 9, but today was the first time I actually played with it. I’m half excited, and half disappointed. My excited half sees that there’s an awful lot to experiment with, and … Continue reading First encounters with Reactive Extensions

"Magic" null argument testing

Warning: here be dragons. I don’t think this is the right way to check for null arguments, but it was an intriguing idea. Today on Stack Overflow, I answered a question about checking null arguments. The questioner was already using an extension similar to my own one in MiscUtil, allowing code like this: public void DoSomething(string name) {     name.ThrowIfNull("name");     // Normal code here } That’s all very well, but it’s annoying to have to repeat the name part. Now in an ideal world, I’d say it would be nice to add an attribute to the parameter and have the … Continue reading "Magic" null argument testing

Contract classes and nested types within interfaces

I’ve just been going through some feedback for the draft copy of the second edition of C# in Depth. In the contracts section, I have an example like this: [ContractClass(typeof(ICaseConverterContracts))] public interface ICaseConverter {     string Convert(string text); } [ContractClassFor(typeof(ICaseConverter))] internal class ICaseConverterContracts : ICaseConverter {     string ICaseConverter.Convert(string text)     {         Contract.Requires(text != null);         Contract.Ensures(Contract.Result<string>() != null);         return default(string);     }     private ICaseConverterContracts() {} } public class InvariantUpperCaseFormatter : ICaseConverter {     public string Convert(string text)      {         return text.ToUpperInvariant();     } } The point is to demonstrate how contracts can be specified for interfaces, and then applied automatically … Continue reading Contract classes and nested types within interfaces

Iterating atomically

The IEnumerable<T> and IEnumerator<T> interfaces in .NET are interesting. They crop up an awful lot, but hardly anyone ever calls them directly – you almost always use a foreach loop to iterate over the collection. That hides all the calls to GetEnumerator(), MoveNext() and Current. Likewise iterator blocks hide the details when you want to implement the interfaces. However, sometimes details matter – such as for this recent Stack Overflow question. The question asks how to create a thread-safe iterator – one that can be called from multiple threads. This is not about iterating over a collection n times independently … Continue reading Iterating atomically

An object lesson in blogging and accuracy; was: Efficient "vote counting" with LINQ to Objects – and the value of nothing

Well, this is embarrassing. Yesterday evening, I excitedly wrote a blog post about an interesting little idea for making a particular type of LINQ query (basically vote counting) efficient. It was an idea that had occurred to me a few months back, but I hadn’t got round to blogging about it. The basic idea was to take a completely empty struct, and use that as the element type in the results of a grouping query – as the struct was empty, it would take no space, therefore "huge" arrays could be created for no cost beyond the fixed array overhead, … Continue reading An object lesson in blogging and accuracy; was: Efficient "vote counting" with LINQ to Objects – and the value of nothing

Generic constraints for enums and delegates

As most readers probably know, C# prohibits generic type constraints from referring to System.Object, System.Enum, System.Array, System.Delegate and System.ValueType. In other words, this method declaration is illegal: public static T[] GetValues<T>() where T : struct, System.Enum {     return (T[]) Enum.GetValues(typeof(T)); } This is a pity, as such a method could be useful. (In fact there are better things we can do… such as returning a read-only collection. That way we don’t have to create a new array each time the method is called.) As far as I can tell, there is no reason why this should be prohibited. Eric Lippert … Continue reading Generic constraints for enums and delegates

The "dream book" for C# and .NET

This morning I showed my hand a little on Twitter. I’ve had a dream for a long time about the ultimate C# book. It’s a dream based on Effective Java, which is my favourite Java book, along with my experiences of writing C# in Depth. Effective Java is written by Josh Bloch, who is an absolute giant in the Java world… and that’s both the problem and the opportunity. There’s no-one of quite the equivalent stature in the .NET world. Instead, there are many very smart people, a lot of whom blog and some of whom have their own books. … Continue reading The "dream book" for C# and .NET

Faking COM to fool the C# compiler

C# 4 has some great features to make programming against COM components bearable fun and exciting. In particular: PIA linking allows you to embed just the relevant bits of the Primary Interop Assembly into your own assembly, so the PIA isn’t actually required at execution time Named arguments and optional parameters make life much simpler for APIs like Office which are full of methods with gazillions of parameters "ref" removal allows you to pass an argument by value even though the parameter is a by-reference parameter (COM only, folks – don’t worry!) Dynamic typing allows you to remove a load … Continue reading Faking COM to fool the C# compiler

A different approach to inappropriate defaults

I’ve had a couple of bug reports about my Protocol Buffers port – both nicely detailed, and one including a patch to fix it. (It’s only due to my lack of timeliness in actually submitting the change that the second bug report occurred. Oops.) The bug was in text formatting (although it also affected parsing). I was using the default ToString behaviour for numbers, which meant that floats and doubles were being formatted as "50,15" in Germany instead of "50.15". The unit tests caught this, but only if you ran them on a machine with an appropriate default culture. Aaargh. … Continue reading A different approach to inappropriate defaults

Go on, ask me anything

This afternoon, I found a comment which had been trapped in the spam bin for this blog. It was from Andrew Rimmer, in reply to my “micro-celebrity” post, pointing me at The world has officially become extremely silly. The surprising thing is, it’s actually useful – at least for me. A number of times I’ve wanted to find my old answers to questions, so I can either just refer to them in a new answer or mark the new question as a dupe. You might have thought that a simple search such as exceptions “jon skeet” would suffice … Continue reading Go on, ask me anything