extension-methods

How do you use extension methods in Web form data-binding expressions?

Has anyone successfully used extension methods in data-binding expressions? Say I have an extension method called "GetName" attached to "MyClass". In the code behind, I have verified this works: MyClass myObject = new MyClass(); MyClass.GetName(); However, in a Web form, I try this: <%@ Import Namespace="My.Namespace" %> Then,...

Was is the performance of the Last() extension method for List<T>?

I really like Last() and would use it all the time for List<T>s. But since it seems to be defined for IEnumerable<T>, I guess it enumerates the enumeration first - this should be O(n) as opposed to O(1) for directly indexing the last element of a List<T>. Are the standard (Linq) extension methods aware of this? The STL in C++ is aware...

Adding extensions methods I think should be in the framework: bad idea?

There's one specific feature that I wish was in the .NET framework, but isn't. I wish there were a DBDataReader.GetString (or GetDateTime, GetInt32, etc.) overload that accepted the field name instead of the index. Using the field name makes for easier maintenance IMO, but the GetXxx methods only accept the field position. I can add th...

Combining extension methods

I'm trying to write 2 extension methods to handle Enum types. One to use the description attribute to give some better explanation to the enum options and a second method to list the enum options and their description to use in a selectlist or some kind of collection. You can read my code up to now here: <Extension()> _ Public ...

Visual Studio's "auto-resolve" feature doesn't work for extension methods - what now?

I love the "Resolve" feature in visual studio. Typical scenario: Type in Debug Type . Notice that no intellisense appears Right-click Select Resolve Choose using System.Diagnostics or System.Diagnostics.Debug Beautiful. Use it all the time. Extension method scenario: Type in var maxNumber = new int[] {1, 2, 3, 4} Type . Notice th...

Can someone help with this generic extension method?

I'm trying to convert an XElement to either null or whatever type T is supplied. Here's what I have so far: public static T? ConvertToNullable<T>(this XElement element) where T : IConvertible { if (element.IsNill()) return null; else return (T)element; } Error: The type T must be a non-nullable value type ...

Extending IEnumerable to Return BindingList

In a previous question on Stack Overflow, I had run into an issue with returning an EF query to the DataGridView. Of course I'd run into an issue. However, I added an extension method that still has me baffled since it isn't working. It seems like it should, but for some reason it's not. public static class BindingListEntityExtension { ...

Is there a workaround for generic type constraint of "special class" Enum in C# 3.0?

Update: See the bottom of this question for a C# workaround. Hi there, Consider the following extension method: public static bool HasFlags<T>(this T value, T flags) where T : System.Enum { // ... } This will, as you may know, throw an error at compile-time, since a class is not normally allowed to inherit from System.En...

Is there a extension library specifically for LINQ to Objects?

This question comes as a logical follow-up to one of my earlier questions: Extension method for UPDATE... I would like to know if there is a library specific for LINQ to Objects ... more specifically a set of useful extensions for IEnumerable ? Here ... I do not want to know about the SQL entity or XML related extensions. Is there an ...

Why do I need the this. qualifier to use an extension method in a master page?

I have the following line of code in some master page code behind, but it fails without the this. Why is that? Repeater rep = this.FindControlsByIdRegEx("maintTableRepeater")[0] as Repeater; This is in the master page Load event, and the extension method is defined as; public static List<Control> FindControlsByIdRegEx(this C...

C#: Repository Methods vs. Extending IQueryable

Hello, I have repositories (e.g. ContactRepository, UserRepository and so forth) which encapsulate data access to the domain model. When I was looking at searching for data, e.g. finding a contact whose first name starts with XYZ a contact whose birthday is after 1960 (etc), I started implementing repository methods such as Firs...

C# Extension Methods only visible and accessible within one class ("private")

Is it possible, in C#, to create extension methods on a class but restrict visibility/accessibility within a class? (e.g. Extension Method A on class M is only accessible within class Z) Example: class A { String foo = ""; String bar = foo.MakeMillionaire("arg"); } In above example I want the extension method "MakeMillionai...

.NET Extenstion Events

I want to add events to some built in .NET classes. For example I want to add the following events to a generic List(T): ItemAdded ItemRemoved ListChanged etc I can think of a few ways to accomplish this, but I'm not sure which approach is best. I could use "extension events", that is, if there is such a thing or a way to use exte...

What overhead is associated with an extension method at runtime? (.NET)

In a data crunching application that is CPU and/or memory access bound, is the overhead of a one line extension method noticable? Is it any higher than a normal function call, or is it simply a compiler/IDE abstraction? For instance, would the following function be ill advised if it was being called upwards of several thousand times a se...

I cannot get my extension method to work (C#)

Any ideas? I marked it as static but it's not working! class ExtensionMethods { public static int Add(this int number, int increment) { return number + increment; } } ...

Overloading a Method to Support Reference Types and Nullable Types

I have an extension method that I would like to overload so it can handle both reference types and nullable value types. When I try to do this, however, I get "Member with the same signature is already declared." Can C# not use the where qualifier on my generic methods to distinguish them from each other? The obvious way to make this w...

C# using params and extension methods

Is the params keyword really not supported within extension methods? I have found that when I create extension methods with the params keyword, that I get "No overloaded method for X takes 2 arguments". Intellisense recognizes the extension method and even knows that it needs an object array. Here's some sample code: public static ...

Useful extension methods C# library for the Date object

Could you guys recommend me a library which handles the most common missing features for the Date object in C#? .. Things like figuring out: What quarter a given date is in Get the start/end of a given quarter/month/week Various subtraction -and addition methods Note: A library which implements its features as extension methods woul...

php extending classes

I'm using a salesforce class called SforceEnterpriseClient. I've referenced that class many places in my application. I want to extend that class to give it the ability to return a single array from a 1 row recordset, right now the record set is about 3 levels deep. There's a few other things I want to do with it as well. I can handl...

IronRuby calling C# Extension Methods - Error - Compatibility in .NET 3.5

I have written an Extension Method off of DataGridView called HideColumns. public static class Extensions { public static void HideColumns(this DataGridView dataGridView, params string[] columnNames) { foreach (string str in columnNames) { if (dataGridView.Columns[str] != null) { ...