Extension methods, explicitly implemented interfaces and collection initializers

This post is the answer to yesterday’s brainteaser. As a reminder, I was asking what purpose this code might have: public static class Extensions  {      public static void Add<T>(this ICollection<T> source, T item)      {          source.Add(item);      }  } There are plenty of answers, varying from completely incorrect (sorry!) to pretty much spot on. As many people noticed, ICollection<T> already has an Add method taking an item of type T. So what difference could this make? Well, consider LinkedList<T>, which implements ICollection<T>, used as below: // Invalid LinkedList<int> list = new LinkedList<int>(); list.Add(10); That’s not valid code (normally)…. whereas this is: // … Continue reading Extension methods, explicitly implemented interfaces and collection initializers

C# 6: First reactions

It’s been a scandalously long time since I’ve blogged about C#, and now that the first C# 6 preview bits are available, that feels like exactly the right thing to set the keys clacking again. Don’t expect anything massively insightful from me just yet; I’d heard Mads and Dustin (individually) talk about some new features of C# 6 at conferences, but this is the first opportunity I’ve had to play with the bits. There are more features to come, and I suspect that the ones we’ve got aren’t in quite the shape they’ll be in the end. First up, if … Continue reading C# 6: First reactions