Interface + Extension (mixin) vs Base Class

Downside of extension methods: clients pre-C#3/VB9 won’t be able to use it as easily. That’s about it as far as I’m concerned – I think the interface-based approach is significantly nicer. You can then mock out your dependencies nicely, and everything is basically less tightly coupled. I’m not a huge fan of class inheritance unless … Read more

Where do I put my extension method?

Consider a class named StringExtensions like so: static class StringExtensions { public static string Truncate(this string value, int maxChars) { return value.Length <= maxChars ? value : value.Substring(0, maxChars) + ” ..”; } } Be sure that whatever namespace you put this class in, you include a using declaration for that namespace. Thus, for a … Read more

Resolving extension methods/LINQ ambiguity

This is probably one of those rare cases where it makes sense to use an extern alias. In the properties page for the reference to System.Core (i.e. under References, select System.Core, right-click and select “Properties”), change the “Aliases” value to “global,SystemCore” (or just “SystemCore” if it’s blank to start with). Then in your code, write: … Read more

Overriding ToString() of List

Perhaps a bit off-topic, but I use a ToDelimitedString extension method which works for any IEnumerable<T>. You can (optionally) specify the delimiter to use and a delegate to perform a custom string conversion for each element: // if you’ve already overridden ToString in your MyClass object… Console.WriteLine(list.ToDelimitedString()); // if you don’t have a custom ToString … Read more

Anonymous Types – Are there any distingushing characteristics?

EDIT: The list below applies to C# anonymous types. VB.NET has different rules – in particular, it can generate mutable anonymous types (and does by default). Jared has pointed out in the comment that the naming style is different, too. Basically this is all pretty fragile… You can’t identify it in a generic constraint, but: … Read more

Extension methods defined on value types cannot be used to create delegates – Why not?

In response to my other answer, Eric Smith correctly notes: “… because it would require implicitly boxing the receiver type parameter …”. Which is what happens anyway, if you do something like this: Func f = 5.ToString; Which is perfectly legal. Thinking about this has led me to a new answer. Try this on for … Read more

Disadvantages of extension methods?

The way that extension methods are imported (i.e. a whole namespace at a time) isn’t granular. You can’t import one extension from a namespace without getting all the rest. It’s not immediately obvious from the source code where the method is defined. This is also an advantage – it means you can make your code … Read more

Using extension methods in .NET 2.0?

Like so: // you need this once (only), and it must be in this namespace namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] public sealed class ExtensionAttribute : Attribute {} } // you can have as many of these as you like, in any namespaces public static class MyExtensionMethods { public static int MeasureDisplayStringWidth ( … Read more