extension-methods

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't be sure if I saw it though, or I am just dreaming. And I can't seem to find an extension...

When should extension methods be avoided?

Before you start pointing me to duplicates just know that I have read nearly all the posts on SO about extension methods. I am just trying to play devil's advocate for a minute to consider the alternative to my working opinion. Recently I was working on a project and a need came up for a method to be a base of an interface. So I suggest...

Comments on a very bad idea

I got an really bad idea :) When exploring extension methods in vb I suddenly thought what about making an extension method on string to execute data access code. <Extension()> Function Execute(ByVal s As String) As Data.DataTable 'make code here to access the database .. 'read connectionstring from the .config file End Function ...

Extension Method in Dynamically Generated Assembly?

I'm trying to include an extension methods static class in a dynamically generated assembly, except that i keep getting a compiler error of 'Type expected' at line 6, column 28, which happens to be on the word 'this'. If i remove 'this' no error is returned (but then it is not an extension method). public static void CodeDomDooDad() ...

Translate LINQ Inline query to extension method

I can't seem to figure out the LINQ Join extension method... I have the following LINQ inline query: var cc = from z in zipcodes join c in contactsRepo.UDF_SelectSome() on z.Zipcode equals c.Zip What is the equivalent of this in LINQ-extension-method syntax? ...

Chaining Extension methods in C#

Is it possible to create an extension method that returns the instance that is invoking the extension method? I would like to have an extension method for anything that inherits from ICollection<T>, returns the object. Much like how jQuery always returns the jquery object. public static object AddItem<T>(this ICollection<T> collection...

How can i do this with Extensions methods or Linq ?

Hello It is a little hard to explain it with my poor english but i will try. In below list sequence, if a item first field has same value with another item first field value but not same second fields. As result i want to collect items which has same first field but not second fields. It looks quite easy but i think it is not any.Cons...

VB.NET: impossible to use Extension method on System.Object instance

Can I make an Extension method for all the subclasses of System.Object (everything)? Example: <Extension> Public Function MyExtension(value As Object) As Object Return value End Function The above functions won't work for object instance: Dim myObj1 As New Object() Dim myObj2 = myObj1.MyExtension() The compiler does not accept...

Sum TimeSpan array with a one-liner?

Is there a way to aggregate multiple aggregates to 1 time span? Dim times = { New TimeSpan(1, 0, 0), New TimeSpan(1, 10, 0), New TimeSpan(1, 50, 0), New TimeSpan(0, 20, 0), New TimeSpan(0, 10, 0) } Dim sum As New TimeSpan For Each ts In times sum = sum.Add(ts) Next 'That's what I desire: sum = times.Sum sum = times.Aggreg...

What is the relation between C# extension methods and the F# pipe-forward operator?

After using F# for a few small problems, I've found it helpful for myself to think of C# extension methods as 'a way of turning the . into a pipe-forward operator'. For example, given a sequence of Int32s named ints, the C# code: ints.Where(i => i > 0) .Select(i => i * i) is similar to the F# code let where = Seq.filter let sele...

Problem Updating the the contents of Dictionary in foreach loop

Hi, I am writing a simple generic update extension for IEnumerable, this method used to join given 2 List of business objects or dictionaries using the given keys and updating the specific field. public static void Update<TOuter, TInner, TKey>(this IEnumerable<TOuter> outer, IEnumerable<TInner> Inner, Func<TOuter, TKey> OuterKeySelecto...

In C#/VB, how do YOU determine whether a method is declared normally or as an extension method?

Extension methods are useful for types that you don't own and can't/don't want to derive from and extend (e.g. reference types and interfaces). Obviously, interfaces should be kept as short and to-the-point as possible, so extension methods for interfaces are particularly useful (e.g. LINQ). For classes, especially classes that you own...

LINQ 2 SQL Method for all tables

My Title may be slightly off but here is what I am trying to do. I have a L2S Method that would be for every table that I would like to write once. This is to set a soft lock column where I will also need a Read and UnLock method. Here is what I have so far: public static void LockRow(string TableName, int TablePrimaryKey) { ...

Passing an extension method to a method expecting a delegate. How does this work?

So at work I was using an API that we didn't write, and one of the methods took a delegate. For one reason or another, the idea came to me that I have an extension method that fits that signature, so I was wondering if it would work. I was sure it wouldn't but much to my surprise, it did. Allow me to demonstrate: Say I have these classe...

Why can I not "see" this enum extension method?

Why cannot I see this enum extension method? (I think I'm going crazy). File1.cs namespace Ns1 { public enum Website : int { Website1 = 0, Website2 } } File2.cs using Ns1; namespace Ns2 { public class MyType : RequestHandler<Request, Response> { public override Res...

Extension method to remove items from a list that exist in another

Hi, I am attempting to filter a list based on a predicate Func. I use Func to filter the complete list and return the result of the operation. I then need to determine which MachineActions appear in the complete list but not the filtered list. static void Main(string[] args) { var allActions = new List<MachineAction> ...

Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)

I can't make any sense of the MSDN documentation for this overload of the Where method that accepts a predicate that has two arguments where the int, supposedly, represents the index of the source element, whatever that means (I thought an enumerable was a sequence and you couldn't see further than the next item, much less do any indexin...

Overloaded VB.NET extension method in class library

It seems that my library of extension methods that was written in VB.NET has problems. I have 2 overloaded extension methods Crop(). When i reference the lib from a VB.NET project i see them. If reference it from a C# project i can't see them. What the hell is going on? ...

CA1026 (all parameters should have default values) and extension methods

Premise When using code analysis (or fxCop) with C# optional parameters you can get a warning of CA1026. The short reason1 for this is not suppling all parameters with a default value. The declaration below rightly generates this warning public Color GetColor(bool red, bool blue = true, bool green = true) However there is a situatio...

Why doesn't the Controls collection provide all of the IEnumerable methods?

I'm not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me. I recently discovered the magic that is extension methods and Linq. Well, I was very sad to find that this isn't valid syntax var c=Controls.Where(x => x.ID=="Some ID").SingleOrDefault(); However from what I can tell, Co...