extension-methods

Can I add extension methods to an existing static class?

I'm a fan of extension methods in C#, but haven't had any success adding an extension method to a static class, such as Console. For example, if I want to add an extension to Console, called 'WriteBlueLine', so that I can go: Console.WriteBlueLine("This text is blue"); I tried this by adding a local, public static method, with Consol...

Is it possible to implement mixins in C#?

I've heard that it's possible with extension methods, but I can't quite figure it out myself. I'd like to see a specific example if possible. Thanks! ...

Will the dynamic keyword in C#4 support extension methods?

I'm listening to a talk about C#4's dynamic keyword and I'm wondering... Will this feature be orthogonal to other .NET features, for example will it support extension methods? public static class StrExtension { public static string twice(this string str) { return str + str; } } ... dynamic x = "Yo"; x.twice(); // will this work? ...

What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

Let's make a list of answers where you post your excellent and favorite extension methods. The requirement is that the full code must be posted and a example and an explanation on how to use it. Based on the high interest in this topic I have setup an Open Source Project called extensionoverflow on Codeplex. Please mark your answers...

How to create an extension method on a class in C#?

I'd like to write an extension method for string, which appears as a static method on System.String - I've not managed to get that done. Is that possible? If so how? Thanks! ...

Enumeration extension methods

In vs2008, is it possible to write an extension methods which would apply to any enumeration. I know you can write extension methods against a specific enumeration, but I want to be able to every enumeration using a single extension method. Is this possible? ...

Define an Extension Method for IEnumerable<T> which returns IEnumerable<T>?

Hi, How do I define an Extension Method for IEnumerable<T> which returns IEnumerable<T>? The goal is to make the Extension Method available for all IEnumerable and IEnumerable<T> where T can be an anonymous type. Kind regards. ...

Linq Extension methods vs Linq syntax

I'm trying to get a handle on if there's a good time to use standard linq keywords or linq extension methods with lambda expressions. They seems to do the same thing, just are written differently. Is it purely a matter of style? var query = from p in Products where p.Name.Contains("foo") orderby c.Name select p; // or wi...

Extension methods usage - Isn't this bad design?

Hi Y'all, I have just started to look at .NET 3.5 so please forgive me if this type of question have been asked before. I am struggling with a decent usage for extension methods, in that I have just downloaded suteki shop an MVC ecommerce offering. In this project there is a pretty standard Repository pattern that extends IRepository. ...

C# casts differing between VS2008 and IIS6

I have a piece of C# code that add the values of an enum to a drop down list by type. It requires that it be called with T1 being an enum type, although I cannot specify this as a type constraint because enums are special case in which this isn't possible. This is not a major concern as this is only used internally and is documented. De...

Do you use any custom ASP.NET MVC HtmlHelper extensions?

I'm interested in seeing what custom extensions other developers have created for the ASP.NET MVC HtmlHelper class. I think Microsoft got off to a great a start, but as usual, left a lot of open holes to fill! Looks like I am going to have to create some for rendering images, rendering action links as images, and so on. Thought ...

Extension method instead of Null Object pattern

I'm wondering to using extension method to avoid checking for null in hierarchy. The example: // GetItems(), GetFirstOrDefault(), GetProduct(), GetIDProduct() are extension methods like: public static SomeType GetSomeProperty( this XYZ obj ) { if ( object.ReferenceEquals( obj, null ) ) { return default( SomeType ); } return obj....

How can I prevent a public class that provides extension methods from appearing in Intellisense?

Is there any way to 'hide' the name of a class, whose sole purpose is to provide extension methods, from Intellisense? I would like to remove the class name from the Intellisense list but need the extension methods of the class to be available to external assemblies via Intellisense in the usual way. ...

C# Reflection to Identify Extension Methods

In C# is there a technique using reflection to determine if a method has been added to a class as an extension method? Given an extension method such as the one shown below is it possible to determine that Reverse() has been added to the string class? public static class StringExtensions { public static string Reverse(this string v...

Using extension methods within inline databinding context

I'm trying to use some extension methods that I use to apply consistent formatting to DateTime and Int32 - which works absolutely fine in code behind, but I'm having issues with databinding. I get: 'System.DateTime' does not contain a definition for 'ToCustomShortDate' for <%# ((ProductionDetails)Container.DataItem).StartDate.ToCust...

Which method performs better: .Any() vs .Count() > 0?

Hi folks, in the System.Linq namespace, we can now extend our IEnumerable's to have theAny() and Count() extension methods. I was told recently that if i want to check that a collection contains 1 or more items inside it, I should use the .Any() extension method instead of the .Count() > 0 extension method because the .Count() extensio...

Disadvantages of extension methods?

Extension method is a really helpful feature that you can add a lot of functions you want in any class. But I am wondering if there is any disadvantage that might bring troubles to me. Any comments or suggestions? ...

Anonymous Types - Are there any distingushing characteristics?

Is there anything to use, to determine if a type is actually a anonymous type? For example an interface, etc? The goal is to create something like the following... //defined like... public static T Get<T>(this IAnonymous obj, string prop) { return (T)obj.GetType().GetProperty(prop).GetValue(obj, null); } //... //And then used like...

Existing LINQ extension method similar to Parallel.For?

The linq extension methods for ienumerable are very handy ... but not that useful if all you want to do is apply some computation to each item in the enumeration without returning anything. So I was wondering if perhaps I was just missing the right method, or if it truly doesn't exist as I'd rather use a built-in version if it's availab...

Is it ok to write my own extension methods in the system namespace?

I've been using extension methods quite a bit recently and have found a lot of uses for them. The only problem I have is remembering where they are and what namespace to use in order to get the extension methods. However, I recently had a thought of writing the extension methods in the System namespace, System.Collections namespace or s...