The irritation of bad names

A couple of days ago I accidentally derailed the comments on Eric Lippert’s blog post about unused "using" directives. The reason that redundant code doesn’t generate a warning in Visual Studio is that it’s what you get to start with in Visual Studio. This led me to rant somewhat about other aspects of Visual Studio’s behaviour which sacrifice long term goodness in favour of short term efficiency. Almost all the subsequent comments (at the time of writing this post) are concerned with my rant rather than Eric’s post. Some agree with me, some don’t – but it’s only now that … Continue reading The irritation of bad names

Type initialization changes in .NET 4.0

This morning, while checking out an email I’d received about my brain-teasers page, I discovered an interesting change to the CLR in .NET 4.0. At least, I think it’s interesting. It’s possible that different builds of the CLR have exhibited different behaviour for a while – I only have 32-bit versions of Windows installed, so that’s what I’m looking at for this whole post. (Oh, and all testing was done under .NET 4.0b2 – it could still change before release.) Note: to try any of this code, build in release mode. Running in the debugger or even running a debug … Continue reading Type initialization changes in .NET 4.0

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

Recent activities

It’s been a little while since I’ve blogged, and quite a lot has been going on. In fact, there are a few things I’d have blogged about already if it weren’t for “things” getting in the way. Rather than writing a whole series of very short blog posts, I thought I’d wrap them all up here… C# in Depth: next MEAP drop available soon – Code Contracts Thanks to everyone who gave feedback on my writing dilemma. For the moment, the plan is to have a whole chapter about Code Contracts, but not include a chapter about Parallel Extensions. My … Continue reading Recent activities

Tricky decisions… Code Contracts and Parallel Extensions in C# in Depth 2nd edition

I’d like some feedback from readers, and I suspect my blog is the simplest way to get it. I’m currently writing chapter 15 of C# in Depth, tentatively about Code Contracts and Parallel Extensions. The problem is that I’m 15 pages in, and I haven’t finished Code Contracts yet. I suspect that with a typesetter moving the listings around a little it can be shortened a little bit, but I’m still concerned. With the amount I’ve still got to write, Code Contracts is going to end up at 20 pages and I expect Parallel Extensions may be 25. That makes … Continue reading Tricky decisions… Code Contracts and Parallel Extensions in C# in Depth 2nd edition

Evil Code of the Day: variance and overloading

(Note that this kind of breakage was mentioned a long time ago in Eric Lippert’s blog, although not in this exact form.) Whenever a conversion becomes available where it wasn’t before, overload resolution can change its behaviour. From C# 1 to C# 2 this happened due to delegate variance with method group conversions – now the same thing is true for generic variance for interfaces. What does the following code print? using System; using System.Collections.Generic; class Base {     public void Foo(IEnumerable<string> strings)     {         Console.WriteLine(“Strings”);     } } class Derived : Base {     public void Foo(IEnumerable<object> objects)     {         Console.WriteLine(“Objects”);     } } class Test {     static void Main()     {         List<string> strings = new List<string>();         new Derived().Foo(strings);     } } The correct answer is “it … Continue reading Evil Code of the Day: variance and overloading

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

Evil code of the day

At a glance, this code doesn’t look particularly evil. What does it do though? Compile it with the C# 4.0b1 compiler and run it… using System; class Base {     public virtual void Foo(int x, int y)     {         Console.WriteLine("Base: x={0}, y={1}", x, y);     } } class Derived : Base {     public override void Foo(int y, int x)     {         Console.WriteLine("Derived: x={0}, y={1}", x, y);     } } class PureEvil {     static void Main()     {         Derived d = new Derived();         Base b = d;                  b.Foo(x: 10, y: 20);         d.Foo(x: 10, y: 20);     } … Continue reading Evil code of the day

OS Jam at Google London: C# 4 and the DLR

Last night I presented for the first time at the Google Open Source Jam at our offices in London. The room was packed, but only a very few attendees were C# developers. I know that C# isn’t the most popular language on the Open Source scene, but I was still surprised there weren’t more people using C# for their jobs and hacking on Ruby/Python/etc at night. All the talks at OSJam are just 5 minutes long, with 2 minutes for questions. I’m really not used to this format, and felt extremely rushed… however, it was still a lot of fun. … Continue reading OS Jam at Google London: C# 4 and the DLR

Dynamic type inference and surprising possibilities

There have been mutterings about the fact that I haven’t been blogging much recently. I’ve been getting down to serious work on the second edition of C# in Depth, and it’s taking a lot of my time. However, I thought I’d share a ghastly little example I’ve just come up with. I’ve been having an email discussion with Sam Ng, Chris Burrows and Eric Lippert about how dynamic typing works. Sam mentioned that even for dynamically bound calls, type inference can fail at compile time. This can only happen for type parameters where none of the dynamic values contribute to … Continue reading Dynamic type inference and surprising possibilities