extension-methods

Are there any plans for "extension properties" in a future version of C#?

I've thought of this before and it came to mind again when reading this question. Are there any plans for "extension properties" in a future version of C#? It seems to me they might be pretty stright-forward to implement with a little more "compiler magic". For example, using get* and set* prefixes on extension method names would turn...

How to conditionally remove items from a .NET collection

Hi all, I'm trying to write an extension method in .NET that will operate on a generic collection, and remove all items from the collection that match a given criteria. This was my first attempt: public static void RemoveWhere<T>(this ICollection<T> Coll, Func<T, bool> Criteria){ foreach (T obj in Coll.Where(Criteria)) Col...

Extension methods Dictionary<TKey,TValue>.RemoveAll? Is it possible?

I've been trying to write an extension method to mimic List.RemoveAll(Predicate). So far I've got this: public static void RemoveAll<TKey,TValue>(this Dictionary<TKey,TValue> dict, Predicate<KeyValuePair<TKey,TValue>> condition) { Dictionary<TKey,TValue> temp = new Dictionary<TKey,TValue>(); ...

Extending the C# Coalesce Operator

Before I explain what I want to do, if you look at the following code, would you understand what it's supposed to do? (updated - see below) Console.WriteLine( Coalesce.UntilNull(getSomeFoo(), f => f.Value) ?? "default value"); C# already has a null-coalescing operator that works quite well on simple objects but doesn't help if you n...

Static extension methods on Seq module

According to this post, F# supports extension methods on object instances and static classes. For example: module CollectionExtensions = type System.Linq.Enumerable with static member RangeChar(first:char, last:char) = {first .. last} open ExtensionFSharp.CollectionExtensions If I type System.Linq.Enumerable., the sta...

When the use of an extension method is a good practice?

Extension methods are really interesting for what they do, but I don't feel 100% confortable with the idea of creating a "class member" outside the class. I prefer to avoid this practice as much as I can, but sometimes it looks better to use extension methods. Which situations you think are good practices of usage for this feature? ...

IKVM and System.Core System.Runtime.CompilerServices.ExtensionAttribute

I'm using the latest release of IKVM to "compile" a Java .jar file into a .NET DLL. That all worked fine, and now I'm trying to reference the DLL in a .NET 3.5 C# project. In my C# project, I've created an static "StringExtensions" class with an extension method on string. For some reason this seemed to work yesterday, but today, I'm ...

List ordering - Extension method not firing!

Hi All, I am trying to sort a List like this: public void Example() { string target = "hello"; List<string> myStings = new List<string>(); myStings.Add("babab"); myStings.Add("Helll"); myStings.Add("atest"); myStings.OrderBy(each => Distance(each, target)); } public int Distance(string stringA, string string...

C#: implicit operator and extension methods

I am trying to create a PredicateBuilder<T> class which wraps an Expression<Func<T, bool>> and provides some methods to easily build up an expression with various And and Or methods. I thought it would be cool if I could use this PredicateBuilder<T> as an Expression<Func<T, bool>> directly, and thought this could be done by having an imp...

What fluent interfaces have you made or seen in C# that were very valuable? What was so great about them?

"Fluent interfaces" is a fairly hot topic these days. C# 3.0 has some nice features (particularly extension methods) that help you make them. FYI, a fluent API means that each method call returns something useful, often the same object you called the method on, so you can keep chaining things. Martin Fowler discusses it with a Java exa...

Extension Method to assign value to a field in every item?

I could have sworn that there was an extension method already built for the Queryable class that I just can't find, but maybe I'm thinking of something different. I'm looking for something along the lines of: IQueryable<Entity> en = from e in IDB.Entities select e; en.ForEach(foo => foo.Status = "Complete"); en.Foreach() would essent...

how to do a program to solve three functions using improve euler's method?

I was given three functions: dx/dt = a(y-x) dy/dt = x(b-z)-y dz/dt = xy-cz All the variables are set by the user. I can do it if its just the basic one-equation problem. But now, i need to make a program using the improved Euler's method. Can the method solve three functions at the same time? Or can I use the Runge-Kutta method? ...

Is this C# extension method impure and if so, bad code?

I'm learning a bit about function programming, and I'm wondering: 1) If my ForEach extension method is pure? The way I'm calling it seems violate the "don't mess with the object getting passed in", right? public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { foreach ( var item in source ) action(item); }...

F# extensions in C#

If you were to define some extension [static, instance] methods, properties in an assembly written in F#, and then use that assembly in C#, would you see the defined extensions in C#? If so, that would be so cool. ...

Extension Method in C# 2.0

What namespace do I need to get my extension to work Here is my Extension Method using System; using System.Collections.Generic; using System.Web; using System.Data; namespace MyUtilities { public static class DataReaderExtensions { public static DataTable ToDataTable(IDataReader reader) { DataTab...

Timeout Pattern - How bad is Thread.Abort really?

I've read at various websites that Thread.Abort is not very good to use. In this case, how do you implement a timeout pattern? For instance, I've read that MS uses the pattern below (which I've wrapped in an extension method) throughout the framework. Personally, I think this is a pretty cool extension, but I'm worried about the Thread.A...

Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?

In the course of my maintenance for an older application that badly violated the cross-thread update rules in winforms, I created the following extension method as a way to quickly fix illegal calls when I've discovered them: /// <summary> /// Execute a method on the control's owning thread. /// </summary> /// <param name="uiElement">Th...

What idiom (if any) do you prefer for naming the "this" parameter to extension methods in C#, and why?

The first parameter to a C# extension method is the instance that the extension method was called on. I have adopted an idiom, without seeing it elsewhere, of calling that variable "self". I would not be surprised at all if others are using that as well. Here's an example: public static void Print(this string self) { if(self != null...

Using reflection to check if a method is "Extension Method"

As part of my application I have a function that receives a MethodInfo and need to do specific operations on it depending if that method is "Extension Method". I've checked the MethodInfo class and I could not find any IsExtension property or flag that shows that the method is extension. Does anyone knows how can I find that from the m...

How can I work around C#'s limitation on calling static functions on a Generic type

I have the following extension method, and would like to make it more generic so I don't have to implement it for every class in our domain. public static IList<User> ToList(this DataTable table) { IList<User> users = new List<User>(); foreach (DataRow row in table.Rows) users.Add(User.FromDataRow(row)); return use...